In many post we have explored Functional Programming concept on different languages being and the focus of the conversation. However, because I have been doing some 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 . F# Scala Java Java Higher Order Functions As explained here higher order functions are simple functions that can receive functions as arguments and can return another function as results. Higher order functions, what are they? In modern , 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 means some kind of . Let’s see how. Java Java interface 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. @FunctionalInterfaceinterface DogAge Integer apply_( )_; { Dog dog } List_< >_ getAges_( < >_ dogs, DogAge f_) {_ List_< >_ ages = new ArrayList_<>()_; Integer List Dog Integer 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 that apply the passed function ( for now) to each dog. getAges interface 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 implementation as we just to do in older . That will be the following way, but please, don’t use it any longer. DogAge Java DogAge dontUseMe = new DogAge_() {_ @Overridepublic Integer apply_( ) {_ return dog.getAge_()_; ; Dog dog }} 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 method. getAges Somehow, is a since it can receive functions as arguments. Java keeps the signature weird by receiving an , but I guess this will be improved in future versions of the language. getAges higher order function interface In order to have a comparison point, let’s define in and look at the differences. Also, we are going to change the name of functions at once so it is more generic. getAges Scala def extractStringFromDogs(dogs: List[Dog], f: Dog => String) =dogs.map(f) in , we could do. Java @FunctionalInterfaceinterface 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 that solves this same problem. That is . In order words, we could do. Java Function<A, B> 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 , we could do the following. Scala scala> def sum(): (Int, Int) => Int = (a, b) => a + b : sum ()(Int, Int) => Int scala> sum() : = $$Lambda$1067/2036949810@715f45c6 res1 (Int, Int) => Int scala> sum()(4,5) : = 9 res2 Int scala> res1(2, 3) : = 5 res3 Int In here, 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? sum Let’s start by defining our own function type ( ) for this particular problem. Functional Interface @FunctionalInterfaceinterface TakeTwo Integer apply_( )_; { Integer a, Integer b } As we could see, is semantically the same as what we defined in . TakeTwo Scala Now, we can define the method again. sum TakeTwo sum_() {_ return -> a + b; ( ) a, b } TakeTwo mySum = ; sum() Integer finalSum = mySum.apply_( )_; 5, 6 This is exactly the same we did in , just that in , the syntax is concise and there is not need to define a to be used as a function type. Yes, the same result is achieved. Scala Scala Functional Interface Again, we don’t actually have to define ourselves since there is an equivalent interface already define in called . By using it we could have written in the following way. TakeTwo Java BiFunction sum BiFunction_< >_ sum_() {_ return -> a + b; Integer, Integer, Integer ( ) a, b } More Functional Interfaces. In order to support the effort of functional programming, incorporates a lot of these . Some of them are: Java Functional Interfaces Consumer Java: public interface Consumer_< > {_ void accept_( )_;....} T T t Scala T => Unit Predicate Java public interface Predicate_< > {_ boolean test_( )_;...} T T t Scala T => boolean Supplier Java public interface Supplier_< > {_ T get_()_; T } Scala :=> T Function Java public interface Function_< > {_ R apply_( )_;...} T, R T t Scala T => R BiFunction Java public interface BiFunction_< > {_ R apply_( )_;...} T, U, R T t, U u Scala (T, U) => R These are only few of the type of functions ( ) that can be found of the new and their counterpart in . Notice that in 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. Functional Interfaces Java Scala Scala Conclusions Somehow, is definitely moving towards Functional Programming and even though the syntax is not the most convenient one, the results are the same. Java syntax, on the other hand, is far more precise and shows better the intend without the need of creating interfaces as function types. Scala I just hope 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. Java