KEMBAR78
Functional Programming and Java8 | PDF
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 Approach
class 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 Layers
class 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

Functional Programming and Java8

  • 1.
  • 2.
    “Every problem incomputer science can be solved by adding another level of indirection.” - David Wheeler
  • 3.
    Programming Paradigms • MachineCode • Procedural Languages • Object Oriented Languages • Functional Languages • Domain Specific Languages
  • 4.
    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.
  • 5.
    Origins • Lambda Calculus(A.Church, 1930s) • Combinatory Logic (H.Curry, 1930s) • LISP (J.McCarthy, 1960s) • ML (1970s) • OCaml (1996) • …
  • 6.
    FP: Why Now? • Moore’s Law runs out (in terms of CPU speeds) • Increasing need for; • Concurrency & Parallelism • Distributed Computing • Scalability
  • 7.
    Benefits of FP •Concurrency/Parallelism without tears • Succinct, concise, understandable programming model • Different programming perspective • Reusability, testability • Becoming more accessible
  • 8.
    Functional Languages • Haskell* • Clean * • Lisp/Scheme • ML/OCaml • F# • Erlang • Java • Scala • C++ (11+) • JavaScript * Pure
  • 9.
    Sample: Imperative Approach class 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));
  • 10.
    Sample: Functional Core +Impure Layers class 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));
  • 11.
    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));
  • 12.
    Hands on examples EnderAydın Orak elron.co
  • 13.
    Done. (for now) EnderAydın Orak elron.co