Open jabrena opened 7 years ago
Books:
https://www.amazon.com/Java-Lambdas-Functional-Programming-Masses/dp/1449370772/ref=pd_sbs_14_t_1?_encoding=UTF8&psc=1&refRID=A30BPKCB6D60170A57R5 https://www.amazon.com/Mastering-Lambdas-Programming-Multicore-Oracle/dp/0071829628/ref=pd_sbs_14_t_2?_encoding=UTF8&psc=1&refRID=A30BPKCB6D60170A57R5 https://www.amazon.co.uk/Functional-Programming-Java-Harnessing-Expressions/dp/1937785467 https://www.manning.com/books/functional-programming-in-java https://pragprog.com/book/vsjava8/functional-programming-in-java
Articles:
http://blog.smartbear.com/programming/7-silly-programming-challenges-to-do-for-fun/ http://www.vasinov.com/blog/16-months-of-functional-programming https://blog.javathlon.com/functional-programming-in-java-8-series-i-a8452fdf81e#.yz2pd326z http://viralpatel.net/blogs/lambda-expressions-java-tutorial/ https://dzone.com/articles/why-we-need-lambda-expressions-0 https://dzone.com/articles/functional-programming-java-8 http://www.drdobbs.com/jvm/lambda-expressions-in-java-8/240166764 https://en.wikipedia.org/wiki/Higher-order_programming https://en.wikipedia.org/wiki/Currying https://www.javacodegeeks.com/2014/03/functional-programming-with-java-8-lambda-expressions-monads.html https://dzone.com/articles/whats-wrong-java-8-part-iii https://dzone.com/articles/whats-wrong-java-8-part-ii https://dzone.com/articles/whats-wrong-java-8-currying-vs http://www.beyondjava.net/blog/java-8-functional-programming-language/ https://jlordiales.me/2014/11/01/overview-java-8/ http://es.slideshare.net/LivePersonDev/functional-programming-with-java-8 https://dzone.com/articles/why-we-need-lambda-expressions-0
package math;
public class Factorial {
public static long declarativeFactorial(int n) {
assert n > 0 : "Argument must be greater than 0";
if (n == 1) return 1;
else return n * declarativeFactorial(n-1);
}
public static long imperativeFactorial(int n) {
assert n > 0 : "Argument must be greater than 0";
long result = 1;
for (int i = 2; i<= n; i++) {
result *= i;
}
return result;
}
}
The Basic Principles of Functional Programming Avoiding Mutable State Functions as First-Class Values
public interface Function1Void { void apply(A a); }
Lambdas and Closures Function1Void
public interface Function2<A1, A2, R> { R apply(A1 a1, A2 a2); }
A closure is formed when the body of a function refers to one or more free variables, variables that aren’t passed in as arguments or defined locally, but are defined in the enclosing scope where the function is defined. The runtime has to “close over” those variables so they are available when the function is actually executed, which could happen long after the original variables have gone out of scope! Java has limited support for closures in inner classes; they can only refer to final variables in the enclosing scope.
Higher-Order Functions Side-Effect-Free Functions Recursion
package functions; import static org.junit.Assert.*; import org.junit.Test; public class RecursionTest { static class Tree { // public fields for simplicity public final Tree left; // left subtree public final Tree right; // right subtree public final int value; // value at this node public Tree(Tree left, int value, Tree right) { this.left = left; this.value = value; this.right = right; } public final String toString() { String leftStr = left == null ? "^" : left.toString(); String rightStr = right == null ? "^" : right.toString(); return "(" + leftStr + "-" + value + "-" + rightStr + ")"; } } @Test public void walkATree() { Tree root = new Tree( new Tree( new Tree(null, 3, null), 2, new Tree(new Tree(null, 5, null), 4, null)), 1, new Tree( new Tree(null, 7, null), 6, new Tree(null, 8, null))); String expected = "(((^-3-^)-2-((^-5-^)-4-^))-1-((^-7-^)-6-(^-8-^)))"; assertEquals(expected, root.toString()); }
Lazy vs. Eager Evaluation Declarative vs. Imperative Programming
public abstract class Option
Exercises:
Pure functional programming uses recursion instead of loops, since a loop counter would have to be mutable.
Exercises
We’ve seen other reasons to avoid mutability. Mutable objects are not thread-safe by default and it’s easy for clients to change their state outside our control. Hence, we should make our objects immutable by removing setter methods and by declaring fields final.
Sometimes we can’t avoid mutation. Since Java doesn’t perform tail-call optimization, declarativeFactorial won’t perform as well as imperativeFactorial. However, we should choose the desirable approach first, then optimize only where actual performance data says we should (since our intuitions are seldom correct). If at all possible, we should keep all public abstractions pure, even when the internals aren’t pure.
The Liskov Substitution Principle
The Liskov Substitution Principle (LSP; see [LSP] and [Martin2003]) provides the correct
way to think about subtyping. Paraphrasing, LSP says that if you have an object of
type T1 with a set of properties, you can only substitute an object of type T2 if it also
conforms to those properties. We say that T2 is a subtype of T1. In Java, a child class
that derives from a parent class is considered a subtype.
Subtyping, Inheritance, and Polymorphism
We sometimes think of subtyping and inheritance as the same thing. Inheritance is used
for subtype polymorphism, where we define type hierarchies with polymorphic behavior.
Inheritance is also sometimes used for implementation inheritance, a form of reuse,
which can cause problems with Liskov substitutability. For completeness, note that
Java’s generics are an example of parametric polymorphism. For example, a List
Exercises
Functional Programming is a concept which treats functions as first class citizens. At the core of functional programming is immutability. It emphasizes on application of functions in contrast to imperative programming style which emphasizes on change in state. https://www.jcp.org/aboutJava/communityprocess/final/jsr335/index.html
Exercises:
https://rosettacode.org/wiki/Rosetta_Code http://www.learntosolveit.com/#java-programming-language https://www.lucidchart.com/techblog/2016/11/22/tracking-data-in-complex-java-code-a-functional-programming-approach/ https://pawelwlodarski.gitbooks.io/functional-programming/content/functions_in_java_-_intro.html https://github.com/PawelWlodarski/workshops-javafp https://pawelwlodarski.gitbooks.io/functional-programming/content/workshop1.html