lunes, 28 de octubre de 2019

Review Functional Programming Java8 Java11

 Review Functional Programming Java8 Java11


https://github.com/jalbertomr/functionalProgramingJava11.git



   On 2014 was delivered a presentation on youtube by IntelliJ with Venkat Subramaniam, https://www.youtube.com/watch?v=Ee5t_EGjv0A&t=573s
, on that was discussed temes like method references, lambda expressions on streams and more.
it was made over java8, Now this is reproducen with java11, to see what change are necessary to made, in this case, only one change was made on Predicate<Integer> isDivisible , on his use in commit marked as
Declarative immutability .range(...).noneMatch(...) isPrime, .test to call Functional Interface on java11

original
    .noneMatch( index -> isDivisible(index))
new
    .noneMatch( index -> isDivisible.test(index))

That's because in the version of java 11 includes the test method., this was the unique change needed for the moment on this exercises.

Excercises

1. Traditional for loop




Output:
false
true
true
false

2. The same, now using Stream, the output is the same.




Output:
false
true
true
false

3. Using a Predicate

Output:
false
true
true
false

4. For vs Stream


Output:
with for : 8
with stream: 8

5.  With out the .get() returns Optional[] which could be Empty value.


Output:
with for : 8
with stream: Optional[8]

6. Using Method References instead of lambda expressions in the stream.


7. Stream show Lazy optimization


Output:
isGreatherThan3 1
isGreatherThan3 2
isGreatherThan3 3
isGreatherThan3 5
isEven 5
isGreatherThan3 4
isEven 4
doubleIt 4
with stream: Optional[8]

8. LAZY optimization don´t do nothing at all until it's required by one … 
   In this case the last instruction on the stream "findFirst" is omitted.

Output:
with stream:

9. Replacing method references with lambdas

Output:
with stream:
isGreatherThan3 1
isGreatherThan3 2
isGreatherThan3 3
isGreatherThan3 5
isEven 5
isGreatherThan3 4
isEven 4
doubleIt 4
Optional[8]

10. Replacing static functions on main class with function into function … 


Output:
with stream:
isEven 5
isEven 4
doubleIt 4
Optional[8]

11.  Generalizing Predicate with Function to receive a parameter 



Output:
with stream:
isEven 5
isEven 4
doubleIt 4
Optional[8]

12.  totalValues sum all values without filter or Selector



Output:
55

13. Dependency Inyection (Strategy Pattern) with OOP composite


Output:
suma todos: 55
suma even: 30

14. Dependency Inyection (Strategy Pattern) with Function composition, Predicate.
 
Output:
suma todos: 55
suma even: 30

15. Pure functions, then can do Referential transparency takes 6sec using stream.

Output:
42

16. Pure functions, then can do Referential transparency takes less than 2 sec using parallelStream.
 
Output:
42

So apparently change from java8 to java11 on functional programming does not represent at the moment a great change on code or implementation. Rest to see the other possible changes that could appear.


eot

No hay comentarios:

Publicar un comentario