Higher order functions are functions that take one or more functions as parameters or return function as result. But why do we learn higher order functions? Because it is fun, no kidding but programming is fun in general–that is why you are reading this post I guess. But seriously speaking, higher order functions can make your code more concise and reusable, and they are just beautiful and elegant. So in this post I am going to talk about some notions in higher order function programming via OCaml(In case you are new to OCaml, see intro to OCaml).
First notion you need to know about is “currying”:
In mathematics and computer science, currying is the technique of translating the evaluation of a function that takes multiple arguments (or a tuple of arguments) into evaluating a sequence of functions, each with a single argument (partial application).
So there is a difference between the following 2 functions:
So the first function takes in a pair of arguments and whenever you want to apply this function you’ll have to provide 2 arguments to it. But the second function takes 1 argument and returns a function that takes in one argument. Confused? See example below:
So fc_demo is the returned function of “fc2 2” and it will take 1 argument and return 2 regardless of the input argument. But this is not saying every time you want to use fc2 you have to apply 1 argument and then apply another one. You can just use it like this:
So you see that currying function could behave the same way as normal functions(or call them first order functions) and can also do more–for creating other functions. (Well, if you want to dig into the currying function, you will see that it is ((fc2 1) 2) with the return value from fc2 being applied to 2 immediately)
(Side note: one of the magics that power currying function is closure: the ability to refer to parent scope variables in a child scope. As long as the child is there, the parent will not get garbage collected. See Wikipedia
OK. that is basically all for the introduction and now we will go into the study of some higher functions.
Map && Filter && All && Some
For those who follows ES 6, you will know there are Array.map and Array.filter now in JS Array prototype. Now we will implement these functions with OCaml. (There are List.map, List.filter and similar functions in List module. We are reimplementing all these functions for study purpose but in practice use of standard library is encouraged)
Fold/Reduce
So now let us now talk about one more complex and more powerful method: fold. In OCaml list there are fold_right and fold_left for different orientation. In JS, there is much similar method called reduce(it is not really the map-reduce’s reduce but works a bit similar to that). The type for fold_left: “(‘a -> ‘b -> ‘a) -> ‘a -> ‘b list -> ‘a = "--so it basically takes in a function, an initial value to build on, a list(the list to apply fold_left on) and return a value of the same type as the initial value. As for the function it takes in as the first parameter, it will take in value from the recursion and one list element, and return a value of the same type as first parameter--and this will be used for next recursion.
Of course higher order functions are not only restricted to lists only. You can use it from tree, graph or any data structure with elegant design. Here is just one example for tree:
There is just so much to explore about higher order functions for you to explore. Have fun coding ;P