SlideShare a Scribd company logo
Booting into FP
Dhaval Dalal
@softwareartisan
https://dhavaldalal.wordpress.com
14th Nov 2019
Our Tour
• Functions, everywhere!

• Understanding Referential Transparency

• What is a Lambda?

• Modularization

• Building Abstractions with Higher-Order Functions

• Lazy Evaluation

• Tackling Complexity using Function Composition

• Streams (Lazy Lists)

• De-structuring and Pattern Matching

• Currying

• Sequence/Chain Operations together using Monads - Dealing with absence of Values

• Overview of Upcoming Sessions
Touted Advantages
Touted Advantages
• Less number of lines of code as compared to imperative code.
• Hence, Functional Programmers are more “productive”
Touted Advantages
• Less number of lines of code as compared to imperative code.
• Hence, Functional Programmers are more “productive”
• Declarative
• Describes “what to do”, rather than imperative - “how to do”
Touted Advantages
• Less number of lines of code as compared to imperative code.
• Hence, Functional Programmers are more “productive”
• Declarative
• Describes “what to do”, rather than imperative - “how to do”
• Immutability
• No assignment statements, a variable value never changes.
Touted Advantages
• Less number of lines of code as compared to imperative code.
• Hence, Functional Programmers are more “productive”
• Declarative
• Describes “what to do”, rather than imperative - “how to do”
• Immutability
• No assignment statements, a variable value never changes.
• No side-effects
• Eliminates major source of bugs, order of evaluation does not matter.
Touted Advantages
• Less number of lines of code as compared to imperative code.
• Hence, Functional Programmers are more “productive”
• Declarative
• Describes “what to do”, rather than imperative - “how to do”
• Immutability
• No assignment statements, a variable value never changes.
• No side-effects
• Eliminates major source of bugs, order of evaluation does not matter.
• Referential Transparency
• Enables equational reasoning
Source: Why Functional Programming Matters - John Hughes
Why FP ^ matters?
really
Why FP ^ matters?
• Its about Modularity
• “Our ability to decompose a problem into parts depends directly on our ability
to glue solutions” - John Hughes
really
Why FP ^ matters?
• Its about Modularity
• “Our ability to decompose a problem into parts depends directly on our ability
to glue solutions” - John Hughes
• Benefits
• Small can be coded quickly and easily.
• Reusability of general-purpose modules.
• Independent testing of modules.
really
Why FP ^ matters?
• Its about Modularity
• “Our ability to decompose a problem into parts depends directly on our ability
to glue solutions” - John Hughes
• Benefits
• Small can be coded quickly and easily.
• Reusability of general-purpose modules.
• Independent testing of modules.
• The Glue
• Lazy Evaluation.
• Higher-Order Functions.
Source: Why Functional Programming Matters - John Hughes
really
Functions Everywhere!
• Function is a smallest unit of computation.
Functions Everywhere!
• Function is a smallest unit of computation.
Functions Everywhere!
f : x ↦ x2OR
f(x) = x2
• Function is a smallest unit of computation.
Functions Everywhere!
f : x ↦ x2OR
f(x) = x2
• Domain, Co-Domain and Range of a function.
• Domain {-3, -2, -1, 0, 1, 2, 3}
• Co-Domain {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
• Function is a smallest unit of computation.
Functions Everywhere!
f : x ↦ x2OR
f(x) = x2
• Domain, Co-Domain and Range of a function.
• Domain {-3, -2, -1, 0, 1, 2, 3}
• Co-Domain {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
• Range {0, 1, 4, 9}
• Function is a smallest unit of computation.
Functions Everywhere!
f : x ↦ x2OR
f(x) = x2
• Domain, Co-Domain and Range of a function.
• Domain {-3, -2, -1, 0, 1, 2, 3}
• Co-Domain {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
• Range {0, 1, 4, 9}
• Function is a relation that maps the domain into co-domain such
that there is exactly one input maps to a output (1:1, can be m:1
but not 1:n).
• Function is a smallest unit of computation.
Functions Everywhere!
f : x ↦ x2OR
f(x) = x2
• Domain, Co-Domain and Range of a function.
• Domain {-3, -2, -1, 0, 1, 2, 3}
• Co-Domain {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
• Range {0, 1, 4, 9}
• Function is a relation that maps the domain into co-domain such
that there is exactly one input maps to a output (1:1, can be m:1
but not 1:n).
• Type of the function is int ↦ int
• Uses nothing other than i/p parameters (and its
definition) to produce o/p 

• Neither modifies input arguments nor reads/modifies
external state

• Call is substitutable by its body. To understand the
code, you don’t have to look elsewhere.
class Calculator {
// (int, int) -> int
public int add(final int x, final int y) {
return x + y;
}
}
A Pure Function
f : T ↦ R
Black-Hole like
Function
• A typical Setter!
class Calculator {
private int memory = 0;
public Calculator(final int memory) {
this.memory = memory;
}
// int -> ()
public void setMemory(final int memory) {
this.memory = memory;
}
}
f : T ↦ ()
Mother’s Love like
Function
• A typical Getter!
class Calculator {
private int memory = 0;
public Calculator(final int memory) {
this.memory = memory;
}
// () -> int
public int recallMemory() {
return memory;
}
}
f : () ↦ R
Politician like
Function
• a Printer!

• Neither consume anything, nor I return anything,
because I do everything under the table.
class Calculator {
private int memory = 0;
public Calculator(final int memory) {
this.memory = memory;
}
public void printMemory() {
System.out.println(memory);
}
}
f : () ↦ ()
• Modifies or interacts with things outside of its scope
(interaction with outside world), and may also return a value.

• Reading/Writing to disk, DB, Socket etc… anything that modifies
external world.
A Side-effecting Function
class Calculator {
private int memory = 0;
public Calculator(final int memory) {
this.memory = memory;
}
public Integer add(final int x, final int y) { … }
public Integer memoryPlus(final int n) {
memory = add(memory, n);
return memory;
}
}
Outside of
its scope
• Write Side-effect…a typical Setter!
Black-Hole like
Side-effecting Function
class Calculator {
private int memory = 0;
public Calculator(final int memory) {
this.memory = memory;
}
// int -> ()
public void setMemory(final int memory) {
this.memory = memory;
}
}
f : T ↦ ()
Mother’s Love like
Side-Effecting Function
• Read Side-effect!
class Calculator {
private Random rnd = new Random();
// () -> int
public int random() {
return rnd.nextInt();
}
}
f : () ↦ R
Politician like
Side-effecting Function
• a Printer!

• Neither consume anything, nor I return anything,
because I do everything under the table.
class Calculator {
private int memory = 0;
public Calculator(final int memory) {
this.memory = memory;
}
public void printMemory() {
System.out.println(memory);
}
}
f : () ↦ ()
Side-effects or Useful-effects?
Source: https://channel9.msdn.com/Blogs/Charles/Simon-Peyton-Jones-Towards-a-Programming-Language-Nirvana
Side-effects or Useful-effects?
Source: https://channel9.msdn.com/Blogs/Charles/Simon-Peyton-Jones-Towards-a-Programming-Language-Nirvana
Understanding
Referential Transparency
class Calculator {
private int memory;
public Calculator(final int memory) {
this.memory = memory;
}
public Integer add(final int x, final int y) {
return x + y;
}
public Integer memoryPlus(final int n) {
memory = add(memory, n);
return memory;
}
}
Understanding
Referential Transparency
c.add(2, 3); // 5
c.add(2, 4); // 6
c.add(2, 3); // 5
c.add(2, 4); // 6
Calculator c = new Calculator();
Referentially Opaque memoryPlus :
1.Cannot replace it with resulting value.
2.Returns different results as time
progresses, as behaviour depends on history.
c.memoryPlus(2); // 2
c.memoryPlus(3); // 5
c.memoryPlus(2); // 7
c.memoryPlus(3); // 10
Time Time
Referentially Transparent add :
1.Substitute any expression with its
resulting value.
2. Returns same results all the time, as
behaviour does not depend on history.
c.memoryPlus(2); // 2
c.memoryPlus(3); // 5
c.memoryPlus(2); // 7
c.memoryPlus(3); // 10
Pure Functions,
Side-Effects and
Evaluation Order
c.add(2, 3); // 5
c.add(2, 4); // 6
c.add(2, 3); // 5
c.add(2, 4); // 6
Calculator c = new Calculator();
Out-of-order execution not
Possible
Time Time
Out-of-order execution possible
How can we make
memoryPlus
Referentially
Transparent?
Ref. Transparent
memoryPlus
class Calculator {
private final int memory;
public Calculator(final int memory) {
this.memory = memory;
}
public int add { … }
public Calculator memoryPlus(final int n) {
return new Calculator(add(memory, n));
}
}
Make memory
Immutable
Return new instance from
operation.
Reflections
• Referential Transparency is about replacing any expression
(or function) with its resulting value.

• To be referentially transparent, function will require to work
with immutable data.

• To be referentially transparent, a function will require to be
pure.

• To be referentially transparent, a function will require to be
deterministic.

• Referentially transparent functions are context-free and the
context is time.
https://github.com/CodeJugalbandi/FunctionalProgramming/tree/master/melodies/referentialTransparency
3 Pillars of FP
• Referential Transparency - enforces invariant expressions or functions
are pure. 

• Substitution Model - RT enables simple and natural mode of
reasoning about program evaluation. 

• Equational Reasoning - Computation proceeds like solving an
algebraic equation, where we substitute and reduce to its simplest
form.
Source: Functional and Reactive Domain Modelling, Debashish Ghosh
Ref.Trans
Substitution
Eq.Reasoning
Good Deeds Happen Anonymously
• Naming is one of the hardest problem in software.

• Alleviate using an anonymous function - Lambda 𝝺.
public int add(final int x, final int y) {
return x + y;
}
public int add(final int x, final int y) {
return x + y;
}
(final int x, final int y) -> { x + y; }
(x, y) -> x + y;
Drop all
inessentials
what remains
are essentials,
parameters and
body
SAMs Become…
new Button().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.print("essence");
}
});
new Button().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("essence");
}
});
new Button().addActionListener(e -> System.out.print("essence"));
Drop all inessentials
what remains
are essentials,
parameters and
body new Button().addActionListener(System.out::print);
OR
Lambda
Function<Integer, Integer> twice = x -> 2 * x;
twice.apply(3); // 6
It would be have been nice if
some more syntactic sugar
was added by Java8 to make
this happentwice(3);Instead of
• Compiled as interface implementation with synthesised
method having signature of the abstract method in that
interface.

• An interface with single abstract method (SAM).

• @FunctionalInterface - Though optional annotation, its better to
have it so that it ensures that it stays as a lambda and not
become something more.
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
…
}
Function<Float, Float> twice = x -> 2 * x;
twice.apply(3); // 6
// A function returns its argument unchanged
Function<Float, Float> identity = x -> x;
@FunctionalInterface
public interface BiFunction<T, U, R> {
R apply(T t, U u);
…
}
BiFunction<Float, Float, Float> add = (x, y) -> x + y;
add.apply(2, 3); // 5
Functional Interfaces
A Black-Hole like
Functional Interface
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
…
}
corrupt.accept(100.34);
@FunctionalInterface
public interface BiConsumer<T, U> {
void accept(T t, U u);
…
}
BiConsumer<Celestial, Celestial> blackHole =
(planet, asteroid) -> { }
blackHole.accept(planet, asteriod);
A Mother’s Love like
Functional Interface
@FunctionalInterface
public interface Supplier<T> {
T get();
}
Supplier<String> mother = () -> “Love";
mother.get(); // Love
Things Emerge
without
consuming anything
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
…
}
Predicate<T> affirmative = x -> true;
affirmative.test(1); // true
Predicate<T> negative = x -> false;
negative.test(1); // false
Predicate<Integer> isEven = x -> x % 2 == 0;
isEven.test(2); // true
isEven.test(3); // false
Mr. Spock like
Functional Interface
Politician like
Functional Interface
@FunctionalInterface
public interface Runnable {
void run();
}
• Neither consume anything, nor I return anything,
because I do everything under the table.
Every“thing” is a
Lambda
• Like Object, a Function is also a thing, so

• Pass a function to a function

• Return a function from within a function

• A function that produces or consumes a function is
called as Higher Order Function - HOF

• Either pass existing method reference (static or instance) or
write an in-place lambda where a method expects a function
parameter.

• Modularization - Build abstractions with HOFs.
Pass a function
to a function
• Subsumed ‘for’ loop.

• Repetitive behaviour using Recursion.
void iterate(int times, Runnable body) {
if (times <= 0) {
return;
}
body.run();
iterate(times - 1, body);
}
iterate(2, () -> System.out.println("Hello"));
// Hello
// Hello
No need for
explicit looping
constructs!
Just a function!
Return a function
from a function
• Subsumed Factory Method.
Function<Double, Double> power(double raiseTo) {
return x -> Math.pow(x, raiseTo);
}
Function<Double, Double> square = power(2.0);
square.apply(2.0); // 4.0
Function<Double, Double> cube = power(3.0);
cube.apply(2.0); // 8.0
Subsumed Aspect
public<T, R> Function<T, R> time(Function<T, R> fn) {
return t -> {
long startTime = System.currentTimeMillis();
R result = fn.apply(t);
long timeTaken = System.currentTimeMillis() - startTime;
System.out.println("Time: " + timeTaken + " ms");
return result;
}
}
• AOP - Around style
Composition Pipeline
• Compose a function from other functions by aligning
types.
• Function composition is not commutative.

• f ∘ g ≠ g ∘ f
// T -> U
Function<T, U> f;
// U -> R
Function<U, R> g;
// T -> U . U -> R = T -> R
Function<T, R> h = f . g;
Why Composition?
• Tackle complexity by composing behaviors.

• Compose larger functions from smaller ones. 

• Smaller functions can be tested independently.

• If the parts are correct, we can then trust the
correctness of the whole.

• Every part of the larger function can be reasoned about
easily.
Composing Functions
• Using compose & andThen
Function<Integer, Integer> square = x -> x * x;
Function<Integer, Integer> twice = x -> 2 * x;
square.compose(twice).apply(2); // 16
square.andThen(twice).apply(2); // 8
Read Left to Right
Think Right to Left
Read Left to Right
Think Left to Right
• For languages that support function composition, look for a way
to go with the grain of thought. In Java, prefer using andThen
Circle of Purity
• Compose behaviours using
pure Functions.

• Data flows through
composed pipes.

• Interact with outside world
using Side-effecting
functions.
Outside
World
Side
Effecting
Functions
Pure
Functions
Courtesy: Venkat
Consumer Supplier
Reflections
• Enforce order of evaluation.

• In imperative programming, statements enforce order of evaluation.

• In FP, the order of composition determines the order of evaluation.

• Function composition (and not function application) is the
default way to build sub-routines in Concatenative
Programming Style, a.k.a Point Free Style.

• Functions neither contain argument types nor names, they
are just laid out as computation pipeline.

• Lot of our domain code is just trying to do this!

• Makes code more succinct and readable.
https://github.com/CodeJugalbandi/FunctionalProgramming/tree/master/melodies/sequencing
Eager Evaluation
• Java uses eager evaluation for method arguments. 

• Args evaluated before passing to the method.
public int first(int x, int y) {

System.out.println("Inside First...");

return x;

}
public int loop() {

System.out.println("Inside Loop...");

return loop();

}

System.out.println(first(10, 20));
// Inside First…
// 10

System.out.println(first(10, loop())); // Stackoverflow

System.out.println(first(loop(), 20)); // Stackoverflow
Lazy Evaluation
• Lazy means don’t compute until there exists a demand.

• Haskell OTOH is lazy by default. 

• Args not evaluated before passing to the method,
instead evaluated upon use.
loop = loop



first x y = x



main :: IO ()

main = do

print $ first 10 20 -- 10

print $ first 10 loop -- 10

print $ first loop 10 -- Infinite Loop
Lazy Evaluation
• Scala is eager by default like Java, and provides a
“lazy” switch.
def loop: Int = loop



def first(x: Int, y: Int) = x



first(10, 20) // 10

first(loop, 20) // Non-termination

first(10, loop) // Non-termination
scala> lazy val x = loop

x: Int = <lazy>

scala> x // Non-termination
public static int first(Supplier<Integer> x, Supplier<Integer> y) {

System.out.println("Inside First...");

return x.get();

}

public static int loop() {

System.out.println("Inside Loop...");

return loop();

}

System.out.println(first(10, 20)); // 10

System.out.println(first(() -> 10, () -> loop())); // 10

System.out.println(first(() -> loop(), () -> 20)); // Stackoverflow
Simulating Lazy Evaluation
Representation of
computation and
not the
computation itself.
• To delay the evaluation of args (lazy), wrap them in lambda.

• Call the lambda when we need to evaluate.

• Immutability and Purity makes lazy evaluation possible.
Imperative Filtering and
Collecting
String sentence = "all mimsy were the borogoves and the momeraths";
String [] words = sentence.split(" ");
StringBuilder caps = new StringBuilder();
for (var word : words) {
if (word.length() < 4) {
caps.append(word.toUpperCase());
caps.append(" ");
}
}
String capitalized = caps.toString().trim();
System.out.println(capitalized); // ALL THE AND THE
Enumeration
and
Filtering
interleaved
Collector
• One of the motivations of Streams is to make
parallelism more accessible to developers.
Streams (Lazy List)
String sentence = "all mimsy were the borogoves and the momeraths";

String capitalized = Stream.of(sentence.split(" "))

.filter(word -> word.length() < 4)

.map(String::toUpperCase)

.collect(Collectors.joining(" "));



System.out.println(capitalized);
String capitalized = Stream.of(sentence.split(" "))

.peek(word -> System.out.println("Filter = " + word))

.filter(word -> word.length() < 4)

.peek(word -> System.out.println("Map = " + word))

.map(String::toUpperCase)

.collect(Collectors.joining(" "));

System.out.println(capitalized);
• One Element at a time passes through the pipeline
public static String capitalize(String s) {

try {

Thread.sleep(2000);

} catch (Exception e) {

}

return s.toUpperCase();

}

String sentence = "all mimsy were the borogoves and the momeraths";

long startTime = System.currentTimeMillis();

String capitalized = Stream.of(sentence.split(" "))

.filter(word -> word.length() < 4)

.map(word -> capitalize(word))

.collect(Collectors.joining(" "));

long timeTaken = System.currentTimeMillis() - startTime;

System.out.println(String.format("%s, Took %d(ms)", capitalized, timeTaken));
// ALL THE AND THE, Took 8043(ms)
How can we improve this?
Parallelizing
public static String capitalize(String s) {

try {

Thread.sleep(2000);

} catch (Exception e) {

}

return s.toUpperCase();

}

String sentence = "all mimsy were the borogoves and the momeraths";

long startTime = System.currentTimeMillis();

String capitalized = Stream.of(sentence.split(" “))
.parallel()
.filter(word -> word.length() < 4)

.map(word -> capitalize(word))

.collect(Collectors.joining(" "));

long timeTaken = System.currentTimeMillis() - startTime;

System.out.println(String.format("%s, Took %d(ms)", capitalized, timeTaken));
// ALL THE AND THE, Took 2027(ms)
• One of the motivations of Streams is to make
parallelism more accessible to developers.
Don’t Parallelize everything
• It takes time to create threads, scatter input and
gather results.
long startTime = System.currentTimeMillis();

long sum = Stream.iterate(0, x -> x + 1)

.limit(10000)

.filter(n -> n % 2 == 0)

.reduce(0, (acc, elem) -> acc + elem);

long diff = System.currentTimeMillis() - startTime;

System.out.println(sum + " Took " + diff + “(ms)");
// 24995000 Took 36(ms)
long startTime = System.currentTimeMillis();

long sum = Stream.iterate(0, x -> x + 1)

.limit(10000)

.parallel()

.filter(n -> n % 2 == 0)

.reduce(0, (acc, elem) -> acc + elem);

long diff = System.currentTimeMillis() - startTime;

System.out.println(sum + " Took " + diff + "(ms)");

// 24995000 Took 56(ms)
Streams - Infinitely Lazy
• Streams are pull-based, consumer decides the pace
of pull.

• With Streams, only essential space is allocated upon
materialization, the rest is in ether :)

• This reduces memory footprint (as you don’t bring every item
in memory).
Stream.iterate(22, x -> x + 1)

.filter(n -> n % 5 == 0)

.findFirst()

.ifPresent(System.out::println); // 25
• Because of laziness findFirst() only runs till it find the
first matching element.
Stream.iterate(22, x -> x + 1);
De-structuring
• It is about extracting data from the innards of the
Data-Structure.

• Gives us a short-hand way of naming parts of the
data-structure.
val list = 1 :: 2 :: 3 :: 4 :: Nil

val first :: second :: rest = list

println(first) // 1

println(second) // 2

println(rest) // List(3, 4)



case class Name(first: String, last: String, salutation: String)



val dd = Name("Dhaval", "Dalal", "Mr.")

val Name(fName, lName, s) = dd

println(fName) // Dhaval

println(lName) // Dalal
Pattern Matching
• In Pattern Matching, we are not only matching on relative
positions of elements in the data-structure, but also trying to
dispatch to different definitions based on the value inside the
data structure.

• This dispatch is a kind of conditional construct. So, it’s about
associating a definition with a particular set of i/ps.
val dd = Name("Dhaval", "Dalal", "Mr.")

val nancy = Name("Nancy", "Drew", "Ms.")

def greet(name: Name) = name match {

case Name("Nancy", "Drew", _) => s"Hello Ms. Drew"

case Name(f@"Dhaval", l, s) => s"Hey $f"

case name => s"Hi ${name.first.toUpperCase}"

}



println(greet(dd)) // Hey Dhaval

println(greet(nancy)) // Hello Ms. Drew

println(greet(Name("Peter", "Shmo", "Mr."))) // Hi PETER
Implementing Recursion
Using Pattern Matching
In Erlang
sum(Acc, []) -> Acc;

sum(Acc, [X|XS]) -> sum(Acc + X, XS).



sum(List) -> sum(0, List).



main(_) ->

io:format("~p~n", [sum([])]),

io:format("~p~n", [sum([1, 2, 3, 4])]).

// (String, Integer) -> String

String merge(String x, Integer y) {

return x + y.toString();

}

// String -> (Integer -> String)

Function<Integer, String> merge(String x) {

return y -> x + y.toString();

}

// () -> (String -> (Integer -> String))

Function<String, Function<Integer, String>> merge() {

return x -> y -> x + y.toString();

}
// (String -> (Integer -> String))

Function<String, Function<Integer, String>> merge = x -> y -> x + y.toString();

System.out.println(merge("Test#", 1)); // Test#1

System.out.println(merge("Test#").apply(2)); // Test#2

System.out.println(merge().apply("Test#").apply(3)); // Test#3
System.out.println(merge.apply("Test#").apply(4)); // Test#4
• Refers to the phenomena of rewriting a N-arg function to a
nest of functions, each taking only 1-arg at a time
Currying
Currying
For each arg, there is
another nested
function, that takes
a arg and returns a
function taking the
subsequent arg, until
all the args are
exhausted.
f : (T, U, V) ↦ R
f′ : T ↦ U ↦ V ↦ R
f = f′
Make Absence Explicit
Optional<T>
• Stop null abuse!

• Don’t return null, use Optional<T> so that it explicitly tells that
in the type.

• A container or view it as a collection containing single
value or is empty.
Presence
Absence
Input(s)
Value
null
Before
String frequentFlyerId = "123456789";

UserRepository userRepository = new UserRepository();

User user = userRepository.findBy(frequentFlyerId);

String targetPage = null;

int miles = 7000;

if (user != null) {

int newPoints = user.addPointsUsing(miles);

targetPage = "http://tierPage";

} else {

targetPage = "http://loginPage";

}

System.out.println(targetPage);
String frequentFlyerId = "123456789";

int miles = 7000;

UserRepository userRepository = new UserRepository();

String targetPage = userRepository.findBy(frequentFlyerId)

.map(user -> {

int newPoints = user.addPointsUsing(miles);

return "http://tierPage";

})

.orElse("http://loginPage");



System.out.println(targetPage);
After
Upcoming Sessions
Thank - You!
https://github.com/DhavalDalal/FunctionalConference-2019-Booting-Into-FP-Demo

More Related Content

PDF
Jumping-with-java8
PDF
Object Design - Part 1
PDF
Currying and Partial Function Application (PFA)
PDF
Lazy java
PDF
Monad Fact #6
PPTX
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
PDF
Javaz. Functional design in Java 8.
PDF
Functional Javascript
Jumping-with-java8
Object Design - Part 1
Currying and Partial Function Application (PFA)
Lazy java
Monad Fact #6
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
Javaz. Functional design in Java 8.
Functional Javascript

What's hot (20)

ODP
Clean code and refactoring
PDF
Clean coding-practices
PDF
DRYing to Monad in Java8
PDF
Pragmatic functional refactoring with java 8
PDF
Creating Lazy stream in CSharp
PDF
Clean code
PDF
Functional Java 8 in everyday life
PDF
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
PDF
Functional go
PPSX
python Function
PPSX
DIWE - Programming with JavaScript
PDF
Introduction to functional programming (In Arabic)
PDF
Functional Python Webinar from October 22nd, 2014
PDF
Java Persistence API
PDF
Dart function - Recursive functions
PPT
3 Function Overloading
PPTX
The Sincerest Form of Flattery
PDF
Reactive Programming for a demanding world: building event-driven and respons...
ODP
From object oriented to functional domain modeling
PPTX
Virtual function
Clean code and refactoring
Clean coding-practices
DRYing to Monad in Java8
Pragmatic functional refactoring with java 8
Creating Lazy stream in CSharp
Clean code
Functional Java 8 in everyday life
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Functional go
python Function
DIWE - Programming with JavaScript
Introduction to functional programming (In Arabic)
Functional Python Webinar from October 22nd, 2014
Java Persistence API
Dart function - Recursive functions
3 Function Overloading
The Sincerest Form of Flattery
Reactive Programming for a demanding world: building event-driven and respons...
From object oriented to functional domain modeling
Virtual function
Ad

Similar to Booting into functional programming (20)

PPTX
The joy of functional programming
PPT
PPT
Basics of cpp
PDF
(3) cpp procedural programming
PPTX
Mastering Python lesson 4_functions_parameters_arguments
PDF
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
PPTX
Functional Programming in Java
PDF
Clojure intro
PPTX
Recursion And Implementation C Programming
PPTX
JS Essence
KEY
Exciting JavaScript - Part I
PPTX
Functional programming in python
PDF
Functional programming in python
PPTX
What the f is a monad?
PDF
Intro to JavaScript - Week 2: Function
PPTX
Developer’s viewpoint on swift programming language
PDF
Python functional programming
PPTX
Introduction to C ++.pptx
PDF
Fp for the oo programmer
PPTX
Functions_new.pptx
The joy of functional programming
Basics of cpp
(3) cpp procedural programming
Mastering Python lesson 4_functions_parameters_arguments
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Programming in Java
Clojure intro
Recursion And Implementation C Programming
JS Essence
Exciting JavaScript - Part I
Functional programming in python
Functional programming in python
What the f is a monad?
Intro to JavaScript - Week 2: Function
Developer’s viewpoint on swift programming language
Python functional programming
Introduction to C ++.pptx
Fp for the oo programmer
Functions_new.pptx
Ad

More from Dhaval Dalal (20)

PDF
Sri-Aurobindos-Integral-Education-Principles.pdf
PDF
Test Pyramid in Microservices Context
PDF
Code Retreat
PDF
Json Viewer Stories
PDF
Value Objects
PDF
Mars rover-extension
PDF
How Is Homeopathy Near To Yoga?
PDF
Approaching ATDD/BDD
PDF
Paradigms Code jugalbandi
PDF
Data Reconciliation
PDF
CodeRetreat
PDF
4-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr2015
PDF
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015
PDF
CodeJugalbandi-Sequencing-HealthyCode-Magazine-Feb-2015
PDF
CodeJugalbandi-Expression-Problem-HealthyCode-Magazine#Jan-2015-Issue
PDF
The tao-of-transformation-workshop
PDF
Grooming with Groovy
PDF
Language portfolio
PDF
Code jugalbandi
PDF
A case-for-graph-db
Sri-Aurobindos-Integral-Education-Principles.pdf
Test Pyramid in Microservices Context
Code Retreat
Json Viewer Stories
Value Objects
Mars rover-extension
How Is Homeopathy Near To Yoga?
Approaching ATDD/BDD
Paradigms Code jugalbandi
Data Reconciliation
CodeRetreat
4-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr2015
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015
CodeJugalbandi-Sequencing-HealthyCode-Magazine-Feb-2015
CodeJugalbandi-Expression-Problem-HealthyCode-Magazine#Jan-2015-Issue
The tao-of-transformation-workshop
Grooming with Groovy
Language portfolio
Code jugalbandi
A case-for-graph-db

Recently uploaded (20)

PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Nekopoi APK 2025 free lastest update
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
L1 - Introduction to python Backend.pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Designing Intelligence for the Shop Floor.pdf
PPTX
Transform Your Business with a Software ERP System
PDF
medical staffing services at VALiNTRY
PPTX
Computer Software and OS of computer science of grade 11.pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Digital Systems & Binary Numbers (comprehensive )
PDF
Cost to Outsource Software Development in 2025
PDF
Understanding Forklifts - TECH EHS Solution
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PTS Company Brochure 2025 (1).pdf.......
Navsoft: AI-Powered Business Solutions & Custom Software Development
CHAPTER 2 - PM Management and IT Context
Odoo Companies in India – Driving Business Transformation.pdf
Nekopoi APK 2025 free lastest update
How to Choose the Right IT Partner for Your Business in Malaysia
L1 - Introduction to python Backend.pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Softaken Excel to vCard Converter Software.pdf
Designing Intelligence for the Shop Floor.pdf
Transform Your Business with a Software ERP System
medical staffing services at VALiNTRY
Computer Software and OS of computer science of grade 11.pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Digital Systems & Binary Numbers (comprehensive )
Cost to Outsource Software Development in 2025
Understanding Forklifts - TECH EHS Solution

Booting into functional programming

  • 1. Booting into FP Dhaval Dalal @softwareartisan https://dhavaldalal.wordpress.com 14th Nov 2019
  • 2. Our Tour • Functions, everywhere! • Understanding Referential Transparency • What is a Lambda? • Modularization • Building Abstractions with Higher-Order Functions • Lazy Evaluation • Tackling Complexity using Function Composition • Streams (Lazy Lists) • De-structuring and Pattern Matching • Currying • Sequence/Chain Operations together using Monads - Dealing with absence of Values • Overview of Upcoming Sessions
  • 4. Touted Advantages • Less number of lines of code as compared to imperative code. • Hence, Functional Programmers are more “productive”
  • 5. Touted Advantages • Less number of lines of code as compared to imperative code. • Hence, Functional Programmers are more “productive” • Declarative • Describes “what to do”, rather than imperative - “how to do”
  • 6. Touted Advantages • Less number of lines of code as compared to imperative code. • Hence, Functional Programmers are more “productive” • Declarative • Describes “what to do”, rather than imperative - “how to do” • Immutability • No assignment statements, a variable value never changes.
  • 7. Touted Advantages • Less number of lines of code as compared to imperative code. • Hence, Functional Programmers are more “productive” • Declarative • Describes “what to do”, rather than imperative - “how to do” • Immutability • No assignment statements, a variable value never changes. • No side-effects • Eliminates major source of bugs, order of evaluation does not matter.
  • 8. Touted Advantages • Less number of lines of code as compared to imperative code. • Hence, Functional Programmers are more “productive” • Declarative • Describes “what to do”, rather than imperative - “how to do” • Immutability • No assignment statements, a variable value never changes. • No side-effects • Eliminates major source of bugs, order of evaluation does not matter. • Referential Transparency • Enables equational reasoning Source: Why Functional Programming Matters - John Hughes
  • 9. Why FP ^ matters? really
  • 10. Why FP ^ matters? • Its about Modularity • “Our ability to decompose a problem into parts depends directly on our ability to glue solutions” - John Hughes really
  • 11. Why FP ^ matters? • Its about Modularity • “Our ability to decompose a problem into parts depends directly on our ability to glue solutions” - John Hughes • Benefits • Small can be coded quickly and easily. • Reusability of general-purpose modules. • Independent testing of modules. really
  • 12. Why FP ^ matters? • Its about Modularity • “Our ability to decompose a problem into parts depends directly on our ability to glue solutions” - John Hughes • Benefits • Small can be coded quickly and easily. • Reusability of general-purpose modules. • Independent testing of modules. • The Glue • Lazy Evaluation. • Higher-Order Functions. Source: Why Functional Programming Matters - John Hughes really
  • 14. • Function is a smallest unit of computation. Functions Everywhere!
  • 15. • Function is a smallest unit of computation. Functions Everywhere! f : x ↦ x2OR f(x) = x2
  • 16. • Function is a smallest unit of computation. Functions Everywhere! f : x ↦ x2OR f(x) = x2 • Domain, Co-Domain and Range of a function. • Domain {-3, -2, -1, 0, 1, 2, 3} • Co-Domain {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
  • 17. • Function is a smallest unit of computation. Functions Everywhere! f : x ↦ x2OR f(x) = x2 • Domain, Co-Domain and Range of a function. • Domain {-3, -2, -1, 0, 1, 2, 3} • Co-Domain {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} • Range {0, 1, 4, 9}
  • 18. • Function is a smallest unit of computation. Functions Everywhere! f : x ↦ x2OR f(x) = x2 • Domain, Co-Domain and Range of a function. • Domain {-3, -2, -1, 0, 1, 2, 3} • Co-Domain {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} • Range {0, 1, 4, 9} • Function is a relation that maps the domain into co-domain such that there is exactly one input maps to a output (1:1, can be m:1 but not 1:n).
  • 19. • Function is a smallest unit of computation. Functions Everywhere! f : x ↦ x2OR f(x) = x2 • Domain, Co-Domain and Range of a function. • Domain {-3, -2, -1, 0, 1, 2, 3} • Co-Domain {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} • Range {0, 1, 4, 9} • Function is a relation that maps the domain into co-domain such that there is exactly one input maps to a output (1:1, can be m:1 but not 1:n). • Type of the function is int ↦ int
  • 20. • Uses nothing other than i/p parameters (and its definition) to produce o/p • Neither modifies input arguments nor reads/modifies external state • Call is substitutable by its body. To understand the code, you don’t have to look elsewhere. class Calculator { // (int, int) -> int public int add(final int x, final int y) { return x + y; } } A Pure Function f : T ↦ R
  • 21. Black-Hole like Function • A typical Setter! class Calculator { private int memory = 0; public Calculator(final int memory) { this.memory = memory; } // int -> () public void setMemory(final int memory) { this.memory = memory; } } f : T ↦ ()
  • 22. Mother’s Love like Function • A typical Getter! class Calculator { private int memory = 0; public Calculator(final int memory) { this.memory = memory; } // () -> int public int recallMemory() { return memory; } } f : () ↦ R
  • 23. Politician like Function • a Printer! • Neither consume anything, nor I return anything, because I do everything under the table. class Calculator { private int memory = 0; public Calculator(final int memory) { this.memory = memory; } public void printMemory() { System.out.println(memory); } } f : () ↦ ()
  • 24. • Modifies or interacts with things outside of its scope (interaction with outside world), and may also return a value. • Reading/Writing to disk, DB, Socket etc… anything that modifies external world. A Side-effecting Function class Calculator { private int memory = 0; public Calculator(final int memory) { this.memory = memory; } public Integer add(final int x, final int y) { … } public Integer memoryPlus(final int n) { memory = add(memory, n); return memory; } } Outside of its scope
  • 25. • Write Side-effect…a typical Setter! Black-Hole like Side-effecting Function class Calculator { private int memory = 0; public Calculator(final int memory) { this.memory = memory; } // int -> () public void setMemory(final int memory) { this.memory = memory; } } f : T ↦ ()
  • 26. Mother’s Love like Side-Effecting Function • Read Side-effect! class Calculator { private Random rnd = new Random(); // () -> int public int random() { return rnd.nextInt(); } } f : () ↦ R
  • 27. Politician like Side-effecting Function • a Printer! • Neither consume anything, nor I return anything, because I do everything under the table. class Calculator { private int memory = 0; public Calculator(final int memory) { this.memory = memory; } public void printMemory() { System.out.println(memory); } } f : () ↦ ()
  • 28. Side-effects or Useful-effects? Source: https://channel9.msdn.com/Blogs/Charles/Simon-Peyton-Jones-Towards-a-Programming-Language-Nirvana
  • 29. Side-effects or Useful-effects? Source: https://channel9.msdn.com/Blogs/Charles/Simon-Peyton-Jones-Towards-a-Programming-Language-Nirvana
  • 30. Understanding Referential Transparency class Calculator { private int memory; public Calculator(final int memory) { this.memory = memory; } public Integer add(final int x, final int y) { return x + y; } public Integer memoryPlus(final int n) { memory = add(memory, n); return memory; } }
  • 31. Understanding Referential Transparency c.add(2, 3); // 5 c.add(2, 4); // 6 c.add(2, 3); // 5 c.add(2, 4); // 6 Calculator c = new Calculator(); Referentially Opaque memoryPlus : 1.Cannot replace it with resulting value. 2.Returns different results as time progresses, as behaviour depends on history. c.memoryPlus(2); // 2 c.memoryPlus(3); // 5 c.memoryPlus(2); // 7 c.memoryPlus(3); // 10 Time Time Referentially Transparent add : 1.Substitute any expression with its resulting value. 2. Returns same results all the time, as behaviour does not depend on history.
  • 32. c.memoryPlus(2); // 2 c.memoryPlus(3); // 5 c.memoryPlus(2); // 7 c.memoryPlus(3); // 10 Pure Functions, Side-Effects and Evaluation Order c.add(2, 3); // 5 c.add(2, 4); // 6 c.add(2, 3); // 5 c.add(2, 4); // 6 Calculator c = new Calculator(); Out-of-order execution not Possible Time Time Out-of-order execution possible
  • 33. How can we make memoryPlus Referentially Transparent?
  • 34. Ref. Transparent memoryPlus class Calculator { private final int memory; public Calculator(final int memory) { this.memory = memory; } public int add { … } public Calculator memoryPlus(final int n) { return new Calculator(add(memory, n)); } } Make memory Immutable Return new instance from operation.
  • 35. Reflections • Referential Transparency is about replacing any expression (or function) with its resulting value. • To be referentially transparent, function will require to work with immutable data. • To be referentially transparent, a function will require to be pure. • To be referentially transparent, a function will require to be deterministic. • Referentially transparent functions are context-free and the context is time. https://github.com/CodeJugalbandi/FunctionalProgramming/tree/master/melodies/referentialTransparency
  • 36. 3 Pillars of FP • Referential Transparency - enforces invariant expressions or functions are pure. • Substitution Model - RT enables simple and natural mode of reasoning about program evaluation. • Equational Reasoning - Computation proceeds like solving an algebraic equation, where we substitute and reduce to its simplest form. Source: Functional and Reactive Domain Modelling, Debashish Ghosh Ref.Trans Substitution Eq.Reasoning
  • 37. Good Deeds Happen Anonymously • Naming is one of the hardest problem in software. • Alleviate using an anonymous function - Lambda 𝝺. public int add(final int x, final int y) { return x + y; } public int add(final int x, final int y) { return x + y; } (final int x, final int y) -> { x + y; } (x, y) -> x + y; Drop all inessentials what remains are essentials, parameters and body
  • 38. SAMs Become… new Button().addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.print("essence"); } }); new Button().addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("essence"); } }); new Button().addActionListener(e -> System.out.print("essence")); Drop all inessentials what remains are essentials, parameters and body new Button().addActionListener(System.out::print); OR
  • 39. Lambda Function<Integer, Integer> twice = x -> 2 * x; twice.apply(3); // 6 It would be have been nice if some more syntactic sugar was added by Java8 to make this happentwice(3);Instead of • Compiled as interface implementation with synthesised method having signature of the abstract method in that interface. • An interface with single abstract method (SAM). • @FunctionalInterface - Though optional annotation, its better to have it so that it ensures that it stays as a lambda and not become something more.
  • 40. @FunctionalInterface public interface Function<T, R> { R apply(T t); … } Function<Float, Float> twice = x -> 2 * x; twice.apply(3); // 6 // A function returns its argument unchanged Function<Float, Float> identity = x -> x; @FunctionalInterface public interface BiFunction<T, U, R> { R apply(T t, U u); … } BiFunction<Float, Float, Float> add = (x, y) -> x + y; add.apply(2, 3); // 5 Functional Interfaces
  • 41. A Black-Hole like Functional Interface @FunctionalInterface public interface Consumer<T> { void accept(T t); … } corrupt.accept(100.34); @FunctionalInterface public interface BiConsumer<T, U> { void accept(T t, U u); … } BiConsumer<Celestial, Celestial> blackHole = (planet, asteroid) -> { } blackHole.accept(planet, asteriod);
  • 42. A Mother’s Love like Functional Interface @FunctionalInterface public interface Supplier<T> { T get(); } Supplier<String> mother = () -> “Love"; mother.get(); // Love Things Emerge without consuming anything
  • 43. @FunctionalInterface public interface Predicate<T> { boolean test(T t); … } Predicate<T> affirmative = x -> true; affirmative.test(1); // true Predicate<T> negative = x -> false; negative.test(1); // false Predicate<Integer> isEven = x -> x % 2 == 0; isEven.test(2); // true isEven.test(3); // false Mr. Spock like Functional Interface
  • 44. Politician like Functional Interface @FunctionalInterface public interface Runnable { void run(); } • Neither consume anything, nor I return anything, because I do everything under the table.
  • 45. Every“thing” is a Lambda • Like Object, a Function is also a thing, so • Pass a function to a function • Return a function from within a function • A function that produces or consumes a function is called as Higher Order Function - HOF • Either pass existing method reference (static or instance) or write an in-place lambda where a method expects a function parameter. • Modularization - Build abstractions with HOFs.
  • 46. Pass a function to a function • Subsumed ‘for’ loop. • Repetitive behaviour using Recursion. void iterate(int times, Runnable body) { if (times <= 0) { return; } body.run(); iterate(times - 1, body); } iterate(2, () -> System.out.println("Hello")); // Hello // Hello No need for explicit looping constructs! Just a function!
  • 47. Return a function from a function • Subsumed Factory Method. Function<Double, Double> power(double raiseTo) { return x -> Math.pow(x, raiseTo); } Function<Double, Double> square = power(2.0); square.apply(2.0); // 4.0 Function<Double, Double> cube = power(3.0); cube.apply(2.0); // 8.0
  • 48. Subsumed Aspect public<T, R> Function<T, R> time(Function<T, R> fn) { return t -> { long startTime = System.currentTimeMillis(); R result = fn.apply(t); long timeTaken = System.currentTimeMillis() - startTime; System.out.println("Time: " + timeTaken + " ms"); return result; } } • AOP - Around style
  • 49. Composition Pipeline • Compose a function from other functions by aligning types. • Function composition is not commutative. • f ∘ g ≠ g ∘ f // T -> U Function<T, U> f; // U -> R Function<U, R> g; // T -> U . U -> R = T -> R Function<T, R> h = f . g;
  • 50. Why Composition? • Tackle complexity by composing behaviors. • Compose larger functions from smaller ones. • Smaller functions can be tested independently. • If the parts are correct, we can then trust the correctness of the whole. • Every part of the larger function can be reasoned about easily.
  • 51. Composing Functions • Using compose & andThen Function<Integer, Integer> square = x -> x * x; Function<Integer, Integer> twice = x -> 2 * x; square.compose(twice).apply(2); // 16 square.andThen(twice).apply(2); // 8 Read Left to Right Think Right to Left Read Left to Right Think Left to Right • For languages that support function composition, look for a way to go with the grain of thought. In Java, prefer using andThen
  • 52. Circle of Purity • Compose behaviours using pure Functions. • Data flows through composed pipes. • Interact with outside world using Side-effecting functions. Outside World Side Effecting Functions Pure Functions Courtesy: Venkat Consumer Supplier
  • 53. Reflections • Enforce order of evaluation. • In imperative programming, statements enforce order of evaluation. • In FP, the order of composition determines the order of evaluation. • Function composition (and not function application) is the default way to build sub-routines in Concatenative Programming Style, a.k.a Point Free Style. • Functions neither contain argument types nor names, they are just laid out as computation pipeline. • Lot of our domain code is just trying to do this! • Makes code more succinct and readable. https://github.com/CodeJugalbandi/FunctionalProgramming/tree/master/melodies/sequencing
  • 54. Eager Evaluation • Java uses eager evaluation for method arguments. • Args evaluated before passing to the method. public int first(int x, int y) {
 System.out.println("Inside First...");
 return x;
 } public int loop() {
 System.out.println("Inside Loop...");
 return loop();
 }
 System.out.println(first(10, 20)); // Inside First… // 10
 System.out.println(first(10, loop())); // Stackoverflow
 System.out.println(first(loop(), 20)); // Stackoverflow
  • 55. Lazy Evaluation • Lazy means don’t compute until there exists a demand. • Haskell OTOH is lazy by default. • Args not evaluated before passing to the method, instead evaluated upon use. loop = loop
 
 first x y = x
 
 main :: IO ()
 main = do
 print $ first 10 20 -- 10
 print $ first 10 loop -- 10
 print $ first loop 10 -- Infinite Loop
  • 56. Lazy Evaluation • Scala is eager by default like Java, and provides a “lazy” switch. def loop: Int = loop
 
 def first(x: Int, y: Int) = x
 
 first(10, 20) // 10
 first(loop, 20) // Non-termination
 first(10, loop) // Non-termination scala> lazy val x = loop
 x: Int = <lazy>
 scala> x // Non-termination
  • 57. public static int first(Supplier<Integer> x, Supplier<Integer> y) {
 System.out.println("Inside First...");
 return x.get();
 }
 public static int loop() {
 System.out.println("Inside Loop...");
 return loop();
 }
 System.out.println(first(10, 20)); // 10
 System.out.println(first(() -> 10, () -> loop())); // 10
 System.out.println(first(() -> loop(), () -> 20)); // Stackoverflow Simulating Lazy Evaluation Representation of computation and not the computation itself. • To delay the evaluation of args (lazy), wrap them in lambda. • Call the lambda when we need to evaluate. • Immutability and Purity makes lazy evaluation possible.
  • 58. Imperative Filtering and Collecting String sentence = "all mimsy were the borogoves and the momeraths"; String [] words = sentence.split(" "); StringBuilder caps = new StringBuilder(); for (var word : words) { if (word.length() < 4) { caps.append(word.toUpperCase()); caps.append(" "); } } String capitalized = caps.toString().trim(); System.out.println(capitalized); // ALL THE AND THE Enumeration and Filtering interleaved Collector • One of the motivations of Streams is to make parallelism more accessible to developers.
  • 59. Streams (Lazy List) String sentence = "all mimsy were the borogoves and the momeraths";
 String capitalized = Stream.of(sentence.split(" "))
 .filter(word -> word.length() < 4)
 .map(String::toUpperCase)
 .collect(Collectors.joining(" "));
 
 System.out.println(capitalized); String capitalized = Stream.of(sentence.split(" "))
 .peek(word -> System.out.println("Filter = " + word))
 .filter(word -> word.length() < 4)
 .peek(word -> System.out.println("Map = " + word))
 .map(String::toUpperCase)
 .collect(Collectors.joining(" "));
 System.out.println(capitalized); • One Element at a time passes through the pipeline
  • 60. public static String capitalize(String s) {
 try {
 Thread.sleep(2000);
 } catch (Exception e) {
 }
 return s.toUpperCase();
 }
 String sentence = "all mimsy were the borogoves and the momeraths";
 long startTime = System.currentTimeMillis();
 String capitalized = Stream.of(sentence.split(" "))
 .filter(word -> word.length() < 4)
 .map(word -> capitalize(word))
 .collect(Collectors.joining(" "));
 long timeTaken = System.currentTimeMillis() - startTime;
 System.out.println(String.format("%s, Took %d(ms)", capitalized, timeTaken)); // ALL THE AND THE, Took 8043(ms) How can we improve this?
  • 61. Parallelizing public static String capitalize(String s) {
 try {
 Thread.sleep(2000);
 } catch (Exception e) {
 }
 return s.toUpperCase();
 }
 String sentence = "all mimsy were the borogoves and the momeraths";
 long startTime = System.currentTimeMillis();
 String capitalized = Stream.of(sentence.split(" “)) .parallel() .filter(word -> word.length() < 4)
 .map(word -> capitalize(word))
 .collect(Collectors.joining(" "));
 long timeTaken = System.currentTimeMillis() - startTime;
 System.out.println(String.format("%s, Took %d(ms)", capitalized, timeTaken)); // ALL THE AND THE, Took 2027(ms) • One of the motivations of Streams is to make parallelism more accessible to developers.
  • 62. Don’t Parallelize everything • It takes time to create threads, scatter input and gather results. long startTime = System.currentTimeMillis();
 long sum = Stream.iterate(0, x -> x + 1)
 .limit(10000)
 .filter(n -> n % 2 == 0)
 .reduce(0, (acc, elem) -> acc + elem);
 long diff = System.currentTimeMillis() - startTime;
 System.out.println(sum + " Took " + diff + “(ms)"); // 24995000 Took 36(ms) long startTime = System.currentTimeMillis();
 long sum = Stream.iterate(0, x -> x + 1)
 .limit(10000)
 .parallel()
 .filter(n -> n % 2 == 0)
 .reduce(0, (acc, elem) -> acc + elem);
 long diff = System.currentTimeMillis() - startTime;
 System.out.println(sum + " Took " + diff + "(ms)");
 // 24995000 Took 56(ms)
  • 63. Streams - Infinitely Lazy • Streams are pull-based, consumer decides the pace of pull. • With Streams, only essential space is allocated upon materialization, the rest is in ether :) • This reduces memory footprint (as you don’t bring every item in memory). Stream.iterate(22, x -> x + 1)
 .filter(n -> n % 5 == 0)
 .findFirst()
 .ifPresent(System.out::println); // 25 • Because of laziness findFirst() only runs till it find the first matching element. Stream.iterate(22, x -> x + 1);
  • 64. De-structuring • It is about extracting data from the innards of the Data-Structure. • Gives us a short-hand way of naming parts of the data-structure. val list = 1 :: 2 :: 3 :: 4 :: Nil
 val first :: second :: rest = list
 println(first) // 1
 println(second) // 2
 println(rest) // List(3, 4)
 
 case class Name(first: String, last: String, salutation: String)
 
 val dd = Name("Dhaval", "Dalal", "Mr.")
 val Name(fName, lName, s) = dd
 println(fName) // Dhaval
 println(lName) // Dalal
  • 65. Pattern Matching • In Pattern Matching, we are not only matching on relative positions of elements in the data-structure, but also trying to dispatch to different definitions based on the value inside the data structure. • This dispatch is a kind of conditional construct. So, it’s about associating a definition with a particular set of i/ps. val dd = Name("Dhaval", "Dalal", "Mr.")
 val nancy = Name("Nancy", "Drew", "Ms.")
 def greet(name: Name) = name match {
 case Name("Nancy", "Drew", _) => s"Hello Ms. Drew"
 case Name(f@"Dhaval", l, s) => s"Hey $f"
 case name => s"Hi ${name.first.toUpperCase}"
 }
 
 println(greet(dd)) // Hey Dhaval
 println(greet(nancy)) // Hello Ms. Drew
 println(greet(Name("Peter", "Shmo", "Mr."))) // Hi PETER
  • 66. Implementing Recursion Using Pattern Matching In Erlang sum(Acc, []) -> Acc;
 sum(Acc, [X|XS]) -> sum(Acc + X, XS).
 
 sum(List) -> sum(0, List).
 
 main(_) ->
 io:format("~p~n", [sum([])]),
 io:format("~p~n", [sum([1, 2, 3, 4])]).

  • 67. // (String, Integer) -> String
 String merge(String x, Integer y) {
 return x + y.toString();
 }
 // String -> (Integer -> String)
 Function<Integer, String> merge(String x) {
 return y -> x + y.toString();
 }
 // () -> (String -> (Integer -> String))
 Function<String, Function<Integer, String>> merge() {
 return x -> y -> x + y.toString();
 } // (String -> (Integer -> String))
 Function<String, Function<Integer, String>> merge = x -> y -> x + y.toString();
 System.out.println(merge("Test#", 1)); // Test#1
 System.out.println(merge("Test#").apply(2)); // Test#2
 System.out.println(merge().apply("Test#").apply(3)); // Test#3 System.out.println(merge.apply("Test#").apply(4)); // Test#4 • Refers to the phenomena of rewriting a N-arg function to a nest of functions, each taking only 1-arg at a time Currying
  • 68. Currying For each arg, there is another nested function, that takes a arg and returns a function taking the subsequent arg, until all the args are exhausted. f : (T, U, V) ↦ R f′ : T ↦ U ↦ V ↦ R f = f′
  • 69. Make Absence Explicit Optional<T> • Stop null abuse! • Don’t return null, use Optional<T> so that it explicitly tells that in the type. • A container or view it as a collection containing single value or is empty. Presence Absence Input(s) Value null
  • 70. Before String frequentFlyerId = "123456789";
 UserRepository userRepository = new UserRepository();
 User user = userRepository.findBy(frequentFlyerId);
 String targetPage = null;
 int miles = 7000;
 if (user != null) {
 int newPoints = user.addPointsUsing(miles);
 targetPage = "http://tierPage";
 } else {
 targetPage = "http://loginPage";
 }
 System.out.println(targetPage); String frequentFlyerId = "123456789";
 int miles = 7000;
 UserRepository userRepository = new UserRepository();
 String targetPage = userRepository.findBy(frequentFlyerId)
 .map(user -> {
 int newPoints = user.addPointsUsing(miles);
 return "http://tierPage";
 })
 .orElse("http://loginPage");
 
 System.out.println(targetPage); After