Functions, Method Reference and Predicates on Java 8
Back to some cool stuff around java 8 and functional programming ideas arriving on the JVM with java lets see some other cool stuff around Lambda.
You can define your functions with a java.util.function.Function interface that allow you to store lambdas in "objects" and receive and pass it trough methods, so this allow you do high order functions.
So lets go down to the code and see some samples around this. There are some samples i coded here with streams, i will cover with more details on other posts.
Functions & Method Reference: A great combination!
The idea here is show you can do here is: you can create methods that receive Functions or Lambdas :-) You can use the Function or Suppliers/Consumers that i showed in a previous post. Here we have a method called func that receives an Supplier of string and just call the method and ir return. So on the main app you can see a call to the func method first using java old approach called inner Classes(technique also know as callbacks). On the line 27 you can see more interesting stuff with lambda or even could be more fun right? Why not use existent methods and libraries on the jvm? with Method Reference you can. Method reference is showed on line 29.
Predicates
So basically predicates are functions that implement test method. This method receive the generic type and return boolean, so here i created a simple predicate that tell if the word in or singular or plural based on a stupid rules contains s, i know it sucks as a rule but the idea here is just show how predicate functions works and how can you combine with streams and filter todo function composition and high order functions.
Given the List of strings called words we use the stream(Collections.stream) in scala you would call this a Sequence or Seq in clojure :-) its the same principle, so we are filtering the collection just to get the words according our predicate and later printing it element on the result with forEach and method reference to the print method.
As you can see there are some cool things on java 8 that enables fun with functional programming ideas and principles.
Cheers,
You can define your functions with a java.util.function.Function interface that allow you to store lambdas in "objects" and receive and pass it trough methods, so this allow you do high order functions.
So lets go down to the code and see some samples around this. There are some samples i coded here with streams, i will cover with more details on other posts.
Functions & Method Reference: A great combination!
The idea here is show you can do here is: you can create methods that receive Functions or Lambdas :-) You can use the Function or Suppliers/Consumers that i showed in a previous post. Here we have a method called func that receives an Supplier of string and just call the method and ir return. So on the main app you can see a call to the func method first using java old approach called inner Classes(technique also know as callbacks). On the line 27 you can see more interesting stuff with lambda or even could be more fun right? Why not use existent methods and libraries on the jvm? with Method Reference you can. Method reference is showed on line 29.
Predicates
So basically predicates are functions that implement test method. This method receive the generic type and return boolean, so here i created a simple predicate that tell if the word in or singular or plural based on a stupid rules contains s, i know it sucks as a rule but the idea here is just show how predicate functions works and how can you combine with streams and filter todo function composition and high order functions.
Given the List of strings called words we use the stream(Collections.stream) in scala you would call this a Sequence or Seq in clojure :-) its the same principle, so we are filtering the collection just to get the words according our predicate and later printing it element on the result with forEach and method reference to the print method.
As you can see there are some cool things on java 8 that enables fun with functional programming ideas and principles.
Cheers,