0% found this document useful (0 votes)
66 views

Functional Programming in Java

The document discusses functional programming and its increasing relevance with Java 8. It defines functional programming as treating computation as mathematical function evaluation that avoids mutable state. Origins include lambda calculus and Lisp. Benefits include easier concurrency, understandability, and reusability. Popular functional languages are listed like Haskell, ML, and newer ones in Java, C++ and JavaScript. Examples show an imperative approach compared to a functional one using immutable data and functions. Overall the document serves as an introduction to functional programming concepts and languages.

Uploaded by

Ender Aydin Orak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Functional Programming in Java

The document discusses functional programming and its increasing relevance with Java 8. It defines functional programming as treating computation as mathematical function evaluation that avoids mutable state. Origins include lambda calculus and Lisp. Benefits include easier concurrency, understandability, and reusability. Popular functional languages are listed like Haskell, ML, and newer ones in Java, C++ and JavaScript. Examples show an imperative approach compared to a functional one using immutable data and functions. Overall the document serves as an introduction to functional programming concepts and languages.

Uploaded by

Ender Aydin Orak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

functional programming

and

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 * • Erlang

• Clean * • Java

• Lisp/Scheme • Scala

• ML/OCaml • C++ (11+)

• F# • 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

You might also like