 In many post we have explored Functional Programming concept on different languages being **_F#_** and **_Scala_** the focus of the conversation. However, because I have been doing some **_Java_** on my workplace, exploring these same concept seems interesting and eyes opening because it has been a long time since last time I seriously used **_Java_**. #### Higher Order Functions As explained here [**_Higher order functions, what are they?_**](https://hackernoon.com/higher-order-functions-what-are-they-be74111659e8) higher order functions are simple functions that can receive functions as arguments and can return another function as results. In modern **_Java_**, we can easily do this. The syntax is not the best, and because there is not type inference we have to explicitly declare the function type which in **_Java_** means some kind of **_interface_**. Let’s see how. First, let’s suppose we have a collections of objects, that is a collection of dogs and we have a function that acts on each dog. We want to be able to invoke this function on each object (dog). Let’s see how we could create such a function. @FunctionalInterface interface DogAge _{_ Integer apply_(_Dog dog_)_; _}_ List_<_Integer_\>_ getAges_(_List_<_Dog_\>_ dogs, DogAge f_) {_ List_<_Integer_\>_ ages = new ArrayList_<>()_; for _(_Dog dog : dogs_) {_ ages.add_(_f.apply_(_dog_))_; _}_ return ages; _}_ We define an interface that given a dog, it extracts some Integer value from it. Then, we define a function **_getAges_** that apply the passed function (**_interface_** for now) to each dog. Now, we have to create the actual function we want to apply over each dog. DogAge f = dog -> dog.getAge_()_; _getAges(_dogs, f_)_; Notice, we don’t have to actually define the **_DogAge_** implementation as we just to do in older **_Java_**. That will be the following way, but please, don’t use it any longer. DogAge dontUseMe = new DogAge_() {_ @Override public Integer apply_(_Dog dog_) {_ return dog.getAge_()_; _} }_; The former is actually generated by the compiler when it sees the first one. We can go one step deeper and do the following. _getAges(_dogs, dog -> dog.getAge_())_; In here we are passing the function right into the **_getAges_** method. Somehow, **_getAges_** is a _higher order function_ since it can receive functions as arguments. Java keeps the signature weird by receiving an **_interface_**, but I guess this will be improved in future versions of the language. In order to have a comparison point, let’s define **_getAges_** in **_Scala_** and look at the differences. Also, we are going to change the name of functions at once so it is more generic. def extractStringFromDogs(dogs: List\[Dog\], f: Dog => String) = dogs.map(f) in **_Java_**, we could do. @FunctionalInterface interface DogMapper _{_ String apply_(_Dog dog_)_; _}_ List<String> extractStringFromDogs(List<Dog> dogs, DogMapper f) { return dogs.stream().map(dog -> f.apply(dog)).collect(Collectors.toList); } It happens that there is a structure already in **_Java_** that solves this same problem. That is **_Function<A, B>_**. In order words, we could do. List<String> extractStringFromDogs(List<Dog> dogs, Function<Dog, String> f) { return dogs.stream().map(dog -> f.apply(dog)).collect(Collectors.toList); } extractStringFromDogs(dogs, dog -> dog.getName()); Now, what about defining functions that actually return other functions? In **_Scala_**, we could do the following. scala> def sum(): (Int, Int) => Int = (a, b) => a + b **sum**: **()(Int, Int) => Int** scala> sum() **res1**: **(Int, Int) => Int** = $$Lambda$1067/2036949810@715f45c6 scala> sum()(4,5) **res2**: **Int** = 9 scala> res1(2, 3) **res3**: **Int** = 5 In here, **_sum_** returns a function that can be stored and evaluated at another time. This is very powerful and important construct of functional languages. Can we do the same in Java? Let’s start by defining our own function type (**_Functional Interface_**) for this particular problem. @FunctionalInterface interface TakeTwo _{_ Integer apply_(_Integer a, Integer b_)_; _}_ As we could see, **_TakeTwo_** is semantically the same as what we defined in **_Scala_**. Now, we can define the **_sum_** method again. TakeTwo sum_() {_ return _(_a, b_)_ \-> a + b; _}_ TakeTwo mySum = _sum()_; Integer finalSum = mySum.apply_(_5, 6_)_; This is exactly the same we did in **_Scala_**, just that in **_Scala_**, the syntax is concise and there is not need to define a **_Functional Interface_** to be used as a function type. Yes, the same result is achieved. Again, we don’t actually have to define **_TakeTwo_** ourselves since there is an equivalent interface already define in **_Java_** called **_BiFunction_**. By using it we could have written **_sum_** in the following way. BiFunction_<_Integer, Integer, Integer_\>_ sum_() {_ return _(_a, b_)_ \-> a + b; _}_ #### More Functional Interfaces. In order to support the effort of functional programming, **_Java_** incorporates a lot of these **_Functional Interfaces_**. Some of them are: #### Consumer _Java:_ public interface Consumer_<_T_\> {_ void accept_(_T t_)_; .... } _Scala_ T => Unit #### Predicate _Java_ public interface Predicate_<_T_\> {_ boolean test_(_T t_)_; ... } _Scala_ T => boolean #### Supplier _Java_ public interface Supplier_<_T_\> {_ T get_()_; _}_ _Scala_ :=> T #### Function _Java_ public interface Function_<_T, R_\> {_ R apply_(_T t_)_; ... } _Scala_ T => R #### BiFunction _Java_ public interface BiFunction_<_T, U, R_\> {_ R apply_(_T t, U u_)_; ... } _Scala_ (T, U) => R These are only few of the type of functions (**_Functional Interfaces_**) that can be found of the new **_Java_** and their counterpart in **_Scala_**. Notice that in **_Scala_** we don’t have to define any interfaces for them, we just have the functions right there and we can define them as we want. #### Conclusions Somehow, **_Java_** is definitely moving towards Functional Programming and even though the syntax is not the most convenient one, the results are the same. **_Scala_** syntax, on the other hand, is far more precise and shows better the intend without the need of creating interfaces as function types. I just hope **_Java_** continues to evolve while reducing verbosity and adding new functional constructs because at the end, we, the engineers, are the one getting real benefits from them.