Questions tagged [higher-order-functions]

Higher-order functions are functions which either take functions as arguments or return them as output (or both). They are a cornerstone of functional programming.

Higher-order functions are functions which either

  1. take functions as arguments or
  2. return them as output

(or both). Functions which do neither of these are known, in contrast, as first-order functions.

Higher-order functions are a cornerstone of the functional programming paradigm, in which the manipulation and transformation of functions is at least as common as the manipulation and transformation of data. That said, several of the most widely-implemented higher-order functions are also found in modern imperative languages. Examples include

  1. map
  2. filter
  3. folding functions (foldl, foldr etc.)
1594 questions
348
votes
6 answers

Understanding the map function

The Python 2 documentation says: Built-in Functions: map(function, iterable, ...) Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that many arguments and is…
Web Master
  • 4,240
  • 6
  • 20
  • 28
335
votes
3 answers

Why is Haskell (GHC) so darn fast?

Haskell (with the GHC compiler) is a lot faster than you'd expect. Used correctly, it can get close-ish to low-level languages. (A favorite thing for Haskellers to do is to try and get within 5% of C (or even beat it, but that means you are using an…
PyRulez
  • 10,513
  • 10
  • 42
  • 87
218
votes
8 answers

difference between foldLeft and reduceLeft in Scala

I have learned the basic difference between foldLeft and reduceLeft foldLeft: initial value has to be passed reduceLeft: takes first element of the collection as initial value throws exception if collection is empty Is there any other difference…
Rajesh Pitty
  • 2,823
  • 2
  • 18
  • 28
217
votes
11 answers

Explode string on commas and trim potential spaces from each value

For example, I would like to create an array from the elements in this string: $str = 'red, green, blue ,orange'; I know you can explode and loop through them and trim: $arr = explode(',', $str); foreach ($arr as $value) { $new_arr[] =…
SeanWM
  • 16,789
  • 7
  • 51
  • 83
163
votes
6 answers

How to use ES6 Fat Arrow to .filter() an array of objects

I'm trying to use ES6 arrow function with .filter to return adults (Jack & Jill). It appears I cannot use an if statement. What do I need to know in order to do this in ES6? var family = [{"name":"Jack", "age": 26}, {"name":"Jill", …
hzhu
  • 3,759
  • 5
  • 29
  • 39
137
votes
11 answers

How does using a function (callback) as an argument to another function work in Python?

Suppose I have some code like: def myfunc(anotherfunc, extraArgs): # somehow call `anotherfunc` here, passing it the `extraArgs` pass I want to pass another existing function as the anotherfunc argument, and a list or tuple of arguments as…
a7664
  • 1,379
  • 2
  • 9
  • 3
96
votes
1 answer

What are paramorphisms?

Reading through this classic paper, I'm stuck on paramorphisms. Unfortunately the section is quite thin, and the Wikipedia page doesn't say anything. My Haskell translation is: para :: (a -> [a] -> b -> b) -> b -> [a] -> b para f base = h where …
Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
87
votes
4 answers

Does PHP have an equivalent to Python's list comprehension syntax?

Python has syntactically sweet list comprehensions: S = [x**2 for x in range(10)] print S; [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] In PHP I would need to do some looping: $output = array(); $Nums = range(0,9); foreach ($Nums as $num) { $out[] =…
86
votes
4 answers

What is `lambda` in Python code? How does it work with `key` arguments to `sorted`, `sum` etc.?

I saw some examples using built-in functions like sorted, sum etc. that use key=lambda. What does lambda mean here? How does it work? For the general computer science concept of a lambda, see What is a lambda (function)?. See also How are lambdas…
Thiru
  • 3,293
  • 7
  • 35
  • 52
85
votes
13 answers

Transposing multidimensional arrays in PHP

How would you flip 90 degrees (transpose) a multidimensional array in PHP? For example: // Start with this array $foo = array( 'a' => array( 1 => 'a1', 2 => 'a2', 3 => 'a3' ), 'b' => array( 1 => 'b1', …
Calvin
  • 4,559
  • 1
  • 25
  • 23
68
votes
2 answers

Difference between using a HOC vs. Component Wrapping

I just checked out HOC's in React. They are pretty cool. However, doesn't simply wrapping a component achieve the same result? Higher Order Component This simple HOC passes state as properties to the ComposedComponent const HOC = ComposedComponent…
Tabbyofjudah
  • 1,973
  • 3
  • 17
  • 29
66
votes
5 answers

What does ".()" mean in Kotlin?

I've seen examples where a function has an argument given by ClassName.() This doesn't seem to be an extension function, which is ClassName.Function() An example is Kotterknife: private val View.viewFinder: View.(Int) -> View? get() = {…
Allan W
  • 2,791
  • 4
  • 23
  • 41
52
votes
3 answers

Select/map each item of a Powershell array to a new array

I have an array of file names in Powershell, and I would like to prepend a path to each of them and get the result in a new array. In C# I could do this using Linq... var files = new string[] { "file1.txt", "file2.txt" }; var path = @"c:\temp\"; var…
Jon Rimmer
  • 12,064
  • 2
  • 19
  • 19
39
votes
7 answers

Creating a new function as return in python function?

I was wondering if it is possible in python to do the following: def func1(a,b): return func2(c,d) What I mean is that suppose I do something with a,b which leads to some coefficients that can define a new function, I want to create this…
arynaq
  • 6,710
  • 9
  • 44
  • 74
38
votes
6 answers

higher level functions in R - is there an official compose operator or curry function?

I can create a compose operator in R: `%c%` = function(x,y)function(...)x(y(...)) To be used like this: > numericNull = is.null %c% numeric > numericNull(myVec) [2] TRUE FALSE but I would like to know if there is an official set of functions…
Alex Brown
  • 41,819
  • 10
  • 94
  • 108
1
2 3
99 100