Download - Functional Programming and Java8
![Page 1: Functional Programming and Java8](https://reader031.vdocuments.us/reader031/viewer/2022021507/587131461a28abf0568b494d/html5/thumbnails/1.jpg)
functional programmingand
Java 8
Ender Aydın Orak elron.co
![Page 2: Functional Programming and Java8](https://reader031.vdocuments.us/reader031/viewer/2022021507/587131461a28abf0568b494d/html5/thumbnails/2.jpg)
“Every problem in computer science can be solved by adding another level of indirection.”
- David Wheeler
![Page 3: Functional Programming and Java8](https://reader031.vdocuments.us/reader031/viewer/2022021507/587131461a28abf0568b494d/html5/thumbnails/3.jpg)
Programming Paradigms
• Machine Code
• Procedural Languages
• Object Oriented Languages
• Functional Languages
• Domain Specific Languages
![Page 4: Functional Programming and Java8](https://reader031.vdocuments.us/reader031/viewer/2022021507/587131461a28abf0568b494d/html5/thumbnails/4.jpg)
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.
![Page 5: Functional Programming and Java8](https://reader031.vdocuments.us/reader031/viewer/2022021507/587131461a28abf0568b494d/html5/thumbnails/5.jpg)
Origins• Lambda Calculus (A.Church, 1930s)
• Combinatory Logic (H.Curry, 1930s)
• LISP (J.McCarthy, 1960s)
• ML (1970s)
• OCaml (1996)
• …
![Page 6: Functional Programming and Java8](https://reader031.vdocuments.us/reader031/viewer/2022021507/587131461a28abf0568b494d/html5/thumbnails/6.jpg)
FP: Why Now ?
• Moore’s Law runs out (in terms of CPU speeds)
• Increasing need for;
• Concurrency & Parallelism
• Distributed Computing
• Scalability
![Page 7: Functional Programming and Java8](https://reader031.vdocuments.us/reader031/viewer/2022021507/587131461a28abf0568b494d/html5/thumbnails/7.jpg)
Benefits of FP
• Concurrency/Parallelism without tears
• Succinct, concise, understandable programming model
• Different programming perspective
• Reusability, testability
• Becoming more accessible
![Page 8: Functional Programming and Java8](https://reader031.vdocuments.us/reader031/viewer/2022021507/587131461a28abf0568b494d/html5/thumbnails/8.jpg)
Functional Languages
• Haskell *
• Clean *
• Lisp/Scheme
• ML/OCaml
• F#
• Erlang
• Java
• Scala
• C++ (11+)
• JavaScript
* Pure
![Page 9: Functional Programming and Java8](https://reader031.vdocuments.us/reader031/viewer/2022021507/587131461a28abf0568b494d/html5/thumbnails/9.jpg)
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));
![Page 10: Functional Programming and Java8](https://reader031.vdocuments.us/reader031/viewer/2022021507/587131461a28abf0568b494d/html5/thumbnails/10.jpg)
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));
![Page 11: Functional Programming and Java8](https://reader031.vdocuments.us/reader031/viewer/2022021507/587131461a28abf0568b494d/html5/thumbnails/11.jpg)
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));
![Page 12: Functional Programming and Java8](https://reader031.vdocuments.us/reader031/viewer/2022021507/587131461a28abf0568b494d/html5/thumbnails/12.jpg)
Hands on examples
Ender Aydın Orak elron.co
![Page 13: Functional Programming and Java8](https://reader031.vdocuments.us/reader031/viewer/2022021507/587131461a28abf0568b494d/html5/thumbnails/13.jpg)
Done. (for now)
Ender Aydın Orak elron.co