functional programming and java8
TRANSCRIPT
functional programmingand
Java 8
Ender Aydın Orak elron.co
“Every problem in computer science can be solved by adding another level of indirection.”
- David Wheeler
Programming Paradigms
• Machine Code
• Procedural Languages
• Object Oriented Languages
• Functional Languages
• Domain Specific Languages
What is FP ?
In computer science, functional programming is a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as
the evaluation of mathematical functions and avoids changing-state and mutable data.
Origins• Lambda Calculus (A.Church, 1930s)
• Combinatory Logic (H.Curry, 1930s)
• LISP (J.McCarthy, 1960s)
• ML (1970s)
• OCaml (1996)
• …
FP: Why Now ?
• Moore’s Law runs out (in terms of CPU speeds)
• Increasing need for;
• Concurrency & Parallelism
• Distributed Computing
• Scalability
Benefits of FP
• Concurrency/Parallelism without tears
• Succinct, concise, understandable programming model
• Different programming perspective
• Reusability, testability
• Becoming more accessible
Functional Languages
• Haskell *
• Clean *
• Lisp/Scheme
• ML/OCaml
• F#
• Erlang
• Java
• Scala
• C++ (11+)
• JavaScript
* Pure
Sample:
Imperative Approachclass Player { String name; int score; Player(..){..} }
void declareWinner(Player p) { System.out.println(p.name + “ is the winner! ”); }
void winner(Player p1, Player p2) { // Impure if(p1.score > p2.score) declareWinner(p1); else declareWinner(p2); }
winner(new Player(“Ram”, 10), new Player(“John”, 20));
Sample:
Functional Core + Impure Layersclass Player { String name; int score; Player(..){..} }
void declareWinner(Player p) { System.out.println(p.name + “ is the winner! ”); }
Player maxScore(Player p1, Player p2) { return p1.score > p2.score ? p1 : p2; }
Player winner(Player p1, Player p2) { declareWinner(maxScore(p1, p2)); }
winner(new Player(“Ram”, 10), new Player(“John”, 20));
Sample:
Reuse
List<Player> players = List(Player("Ram", 10), Player("John", 15), Player("Hari", 20),
Player("Krishna", 17));
System.out.println(players.stream().reduce(Play::maxScore));
Hands on examples
Ender Aydın Orak elron.co
Done. (for now)
Ender Aydın Orak elron.co