SlideShare a Scribd company logo
1 of 72
Download to read offline
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

What's hot

What's hot (20)

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

Similar to Booting into functional programming

Similar to Booting into functional programming (20)

The joy of functional programming
The joy of functional programmingThe joy of functional programming
The joy of functional programming
 
Modular programming
Modular programmingModular programming
Modular programming
 
Basics of cpp
Basics of cppBasics of cpp
Basics of cpp
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programming
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_arguments
 
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
Functional Patterns for C++ Multithreading (C++ Dev Meetup Iasi)
 
Functional Programming in Java
Functional Programming in JavaFunctional Programming in Java
Functional Programming in Java
 
Clojure intro
Clojure introClojure intro
Clojure intro
 
JS Essence
JS EssenceJS Essence
JS Essence
 
Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part I
 
Functional programming in python
Functional programming in pythonFunctional programming in python
Functional programming in python
 
Functional programming in python
Functional programming in pythonFunctional programming in python
Functional programming in python
 
What the f is a monad?
What the f is a monad?What the f is a monad?
What the f is a monad?
 
Intro to JavaScript - Week 2: Function
Intro to JavaScript - Week 2: FunctionIntro to JavaScript - Week 2: Function
Intro to JavaScript - Week 2: Function
 
Developer’s viewpoint on swift programming language
Developer’s viewpoint on swift programming languageDeveloper’s viewpoint on swift programming language
Developer’s viewpoint on swift programming language
 
Python functional programming
Python functional programmingPython functional programming
Python functional programming
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
Fp for the oo programmer
Fp for the oo programmerFp for the oo programmer
Fp for the oo programmer
 
Functions_new.pptx
Functions_new.pptxFunctions_new.pptx
Functions_new.pptx
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 

More from Dhaval Dalal

More from Dhaval Dalal (20)

Test Pyramid in Microservices Context
Test Pyramid in Microservices ContextTest Pyramid in Microservices Context
Test Pyramid in Microservices Context
 
Code Retreat
Code RetreatCode Retreat
Code Retreat
 
Json Viewer Stories
Json Viewer StoriesJson Viewer Stories
Json Viewer Stories
 
Value Objects
Value ObjectsValue Objects
Value Objects
 
Mars rover-extension
Mars rover-extensionMars rover-extension
Mars rover-extension
 
How Is Homeopathy Near To Yoga?
How Is Homeopathy Near To Yoga?How Is Homeopathy Near To Yoga?
How Is Homeopathy Near To Yoga?
 
Approaching ATDD/BDD
Approaching ATDD/BDDApproaching ATDD/BDD
Approaching ATDD/BDD
 
Paradigms Code jugalbandi
Paradigms Code jugalbandiParadigms Code jugalbandi
Paradigms Code jugalbandi
 
Data Reconciliation
Data ReconciliationData Reconciliation
Data Reconciliation
 
CodeRetreat
CodeRetreatCodeRetreat
CodeRetreat
 
4-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr2015
4-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr20154-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr2015
4-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr2015
 
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar20153-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015
 
CodeJugalbandi-Sequencing-HealthyCode-Magazine-Feb-2015
CodeJugalbandi-Sequencing-HealthyCode-Magazine-Feb-2015CodeJugalbandi-Sequencing-HealthyCode-Magazine-Feb-2015
CodeJugalbandi-Sequencing-HealthyCode-Magazine-Feb-2015
 
CodeJugalbandi-Expression-Problem-HealthyCode-Magazine#Jan-2015-Issue
CodeJugalbandi-Expression-Problem-HealthyCode-Magazine#Jan-2015-IssueCodeJugalbandi-Expression-Problem-HealthyCode-Magazine#Jan-2015-Issue
CodeJugalbandi-Expression-Problem-HealthyCode-Magazine#Jan-2015-Issue
 
The tao-of-transformation-workshop
The tao-of-transformation-workshopThe tao-of-transformation-workshop
The tao-of-transformation-workshop
 
Grooming with Groovy
Grooming with GroovyGrooming with Groovy
Grooming with Groovy
 
Language portfolio
Language portfolioLanguage portfolio
Language portfolio
 
Code jugalbandi
Code jugalbandiCode jugalbandi
Code jugalbandi
 
A case-for-graph-db
A case-for-graph-dbA case-for-graph-db
A case-for-graph-db
 
Transition contours
Transition contoursTransition contours
Transition contours
 

Recently uploaded

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 

Recently uploaded (20)

Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 

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