In mathematics and computer science, a higher-order function (also functional, functional form or functor; not to be confused with the functor concept in category theory) is a function that does at least one of the following: 1. takes one or more functions as arguments, 2. returns a function as its result. That is the formal definition, but what really are they and why do we need them? Sometimes, it is important to execute some kind of operation on the items of a dataset, but this particular operation is defined at execution time. Sometimes there is just no way to know what action we want to take except that we need to do something at some particular point, but it is completely unknown. Let’s see an example. let x = [1..100] for each item in x doprint(item) Here, we are printing the each number in the list x by calling the function print. What about if we need to do something else with each item? Do we need to implement all possible cases that we think we will use? Let’s define a that will be called for each number (item) in x. function let succ (x) -> x + 1 We have defined the function which basically gets a value and returns its successor. The domain of is the Int numbers and that can be inferred from the body of since we are adding 1 to the argument and we say that has type successor succ succ succ Int -> Int. Now, let’s review the previous example. let map (f, list) =for each item in list dof(item) Here, is a higher order function because it received a function as an argument, which will be executed on each item from list. At the moment we define we don’t need to know what will be the value of , except that as type . There is no way to infer the return value of from the previous code, but we know it has to receive an . map f map f f Int -> Some f Int Another example will be a function that returns another function. Let’s review it. let plus2 (x) =let succ (y) = y + 1 succ (succ x) let r = plus2 (1) In this case is a function of type or . It basically takes an and returns a function that takes another and returns an . will be equals 3 after evaluation. plus2 Int -> Int -> Int Int -> (Int -> Int) Int Int Int r Now that we have shown how these kinds of functions are, let’s see how modern programming languages define them. In we can define a function of type as follow: Scala map (a -> b, List[a]) -> List[b] def map[a,b](f: a => b, v: List[a]): List[b] = {for ( i <- v )yield f(i)} val squares = map( (x: Int) => x * x, 1.to(100).toList ) For each in we apply the function and return its value. The variable squares will have the squares values of the numbers from 1 to 100. Please note that we are defining the function at the moment we call , so doesn’t know anything about the function . We can easily say: i v f squared map map f def succ (x: Int): Int = x + 1 val successors = map(succ, 1.to(100).toList) We just changed the function passed to and we get a completed different result. This level of abstraction is what makes , as a higher order function, very powerful. map map Even programming languages that are not functional, such as , have added these abstractions so they can be flexible to many use cases. C# In we need to implement a in order to get the same functionality, but it brings the same value to the language. Because higher or functions are so important, the .NET Framework has some already implemented, so let’s use them C# delegate delegates List<b> map<a>(Func<a,b> f, List<a> list){foreach (a x in list){yield return f(x);}} var successors = map(x => x + 1, someIntList); We have achieved the same functionality of the in . The only different is that does not have higher order functions and it has to use the constructs, yet we get the same functionality as result. Note that in there is no need to declare the type of the arguments of the function when we pass it to , infers it! map Scala C# delegates C# succ map C# In is where it gets very interesting because the language support to the functional programming (FP) paradigm. In we do: F# F# let map f = function|[] ->[]|h::t -> (f h) @ (t |> map f) let successors = [1..100] |> map (fun x -> x + 1) Seems complicated, but it is in fact the same we saw previously. The only additions are the functional programming style and the tail recursion, typical of functional programming languages. The pipe operator is just a language construct, we might say instead or . Note that the type inference system works quite nice in . We have not defined any types; however, is inferred to receive a function and a list and returns a list. map |> map f t map (fun x -> x + 1) [1..100] F# map transformed The function in will be as follow: plus2 F# let plus2 x =let succ y = y + 1succ succ y For some people, higher order functions are common in their lives. These functions are built in within many languages and frameworks; still, I have seen some confusion around them. Languages that do not support them need to find alternatives that sometimes are not easy to use and implement. The language uses pointers to functions, uses and others use objects as substitutes. On the other hand, there are those that support higher order function out of the box. , , and are just a few examples. C C# delegates Scala Python F# Even though sometimes we use these functions without thinking much about them, we should be aware of how they are implemented so we can benefit even more from them. Those who are new to this programming construct we inherited from the calculus should study them and get familiar around them since the trend of functional programming seems to be gaining some battles. Higher order functions are some of the pillars of the FP languages and that seems to be the future. lambda is how hackers start their afternoons. We’re a part of the family. We are now and happy to opportunities. Hacker Noon @AMI accepting submissions discuss advertising &sponsorship To learn more, , , or simply, read our about page like/message us on Facebook tweet/DM @HackerNoon. If you enjoyed this story, we recommend reading our and . Until next time, don’t take the realities of the world for granted! latest tech stories trending tech stories