SlideShare a Scribd company logo
Functional Programming 101
Agenda
What is FP
Basics of FP
Pure Functions
Immutability
Referential Transparency
Higher Order Functions
Partial Application and Currying
What is FP ?
In functional programming, we place a major emphasis on
writing code using functions as building blocks. Your program is
defined in terms of one main function. This main function is
defined in terms of other functions, which are in turn defined in
terms of still more functions, until at the bottom level the
functions are just language primitives like number or string.
What is FP ?
Basics
Every Software program has two things
● Behavior(What the program does)
● Data
In FP , behavior is handled only using functions and data is
immutable. Rather than changing data they take in, functions in
functional programming take in data as input and produce new
values as output(always). Also functions in FP can be passed
around just like data.
Basics
Functions
● Functions are pure
● Functions use immutable data
● Functions guarantee referential transparency
● Functions are first-class entities
Pure Functions
Qualities of Pure Functions
● The function depends only on the input provided to it to
produce a result (and not on any external state).
● The function does not cause any observable side effects,
such as modifying a global object or modifying a parameter
passed by reference
Pure Functions
var PI = 3.14;
const calculateArea = (radius) => radius * radius * PI;
let count = 1;
const increment = (val) => count = count + val;
const reverseArray = (array) => array.reverse();
Pure Functions
async function amountExceedsCreditLimit(amount) {
const limit = await getCreditLimit() // assume api call
if (amount > limit) {
return true;
}
return false;
}
console.log(“Hello World!”);
Pure Functions benefits
● Pure functions are predictable
● Pure functions are easy to test
● Easier to refactor
Immutability
var firstName = “Maneesh”
firstName[3] = “i”;
console.log(firstName[3]);
console.log(firstName);
firstName = firstName.slice(0,3);
console.log(firstName);
Immutability
Mutation modifies the underlying object without affecting the
link between the variable and the object, whereas assignment
changes the link to a different object, but doesn’t modify any
objects.
Functions -Referential
Transparency
Functions -Referential
Transparency
If a function consistently yields the same result for the same
input , it is referentially transparent. Referential transparency
gives the ability to freely replace an expression with its value
and not change the behavior of the program.
Functions -Referential
Transparency
Pure functions + immutable data = referential transparency
i.e a function that doesn't rely on external states or mutable
data will always return the same output for a given input.
Functions are first class Citizens
● Refer to it from constants and variables
● Pass it as a parameter to other functions
● Return it as result from other functions
A higher order function is a function that meets at least one of
the following requirements: Note most functions which take a
callback are higher order functions.
● It accepts a function as an argument
● It returns a new function
Higher Order Functions
Benefits of higher order functions
● Hide Implementation Details
numbers.stream().map(s -> Integer.valueOf(s))
.filter(number -> number % 2 == 0)
.collect(Collectors.toList());
Higher Order Functions
Benefits of higher order functions
● Adding Functionality to an Existing Function
function logResult(fn) {
return (...args) => { const result = fn(...args)
console.log(result)
return result
}}
Higher Order Functions
const findAndMultiply = (index,multiplier) => {
return (array) => {
if (array && typeof(array[index] == ‘number’) {
return array[index] * multiplier ;
else{
Return “Bhaiya, ye na ho pai”’
}
};
};
Higher Order Functions
Function Composition
Create small reusable functions that you can combine to
compose new functions.
BiFunction<String, List<Article>, List<Article>> byAuthor =
(name, articles) -> articles.stream()
.filter(a -> a.getAuthor().equals(name))
.collect(Collectors.toList());
Function Composition
BiFunction<String, List<Article>, List<Article>> byTag =
(tag, articles) -> articles.stream()
.filter(a -> a.getTags().contains(tag))
.collect(Collectors.toList());
Function<List<Article>, List<Article>> sortByDate =
articles -> articles.stream()
.sorted((x, y) -> y.published().compareTo(x.published()))
.collect(Collectors.toList());
Function Composition
Function<List<Article>, Optional<Article>> first =
a -> a.stream().findFirst();
Function<List<Article>, Optional<Article>> newest =
first.compose(sortByDate);
BiFunction<String, List<Article>, List<Article>>
byAuthorSorted = byAuthor.andThen(sortByDate);
Function Composition
The compose method uses a parameter named, before, and the
andThen method uses a parameter named, after. These names
indicate the order that the functions will be evaluated on.
f(x) = (2+x)*3
g(x) = 2 +(x*3)
Use compose and andThen to form f(x) and g(x).
Function Composition
Function<Integer, Integer> baseFunction = t -> t + 2;
Function<Integer, Integer> afterFunction =
baseFunction.andThen(t -> t * 3);
System.out.println(afterFunction.apply(5));
Function<Integer, Integer> beforeFunction =
baseFunction.compose(t -> t * 3);
System.out.println(beforeFunction.apply(5));
when the apply method is executed, the corresponding lambda
expression is executed
Function Composition
int hoursWorked[] = {8, 12, 8, 6, 6, 5, 6, 0};
int totalHoursWorked = Arrays.stream(hoursWorked)
.filter(n -> n > 6)
.sum();
LocalDateTime timeInstance = LocalDateTime.now()
.plusDays(3)
.minusHours(4)
.plusWeeks(1)
.plusYears(2);
Fluent Interfaces
Partial application is a way to turn a function that expects
multiple parameters into one that will keep returning a new
function until it receives all its arguments
const f = (a,b,c) => a * b *c
const g = partial(f,[2,3])
const h = g(4)
Partial Application
const applyTax = (amount,tax) => amount + (amount * tax)
applyTax(0.08) =>
applyTaxOfEightPercent = partial(applyTax,[0.08]);
applyTaxOfFivePercent = partial(applyTax,[0.05]);
applyTaxofEightPercent(100);
applyTaxofFivePercent(100);
Exercise - Template design pattern. Try implementing it using
partial application.
Partial Application
Currying is the process of taking a function that accepts N
arguments and turning it into a chained series of N functions
that each take one argument. The difference between partial
application and currying is that with currying, you can never
provide more than one argument — it has to be either one or
zero. If a function takes 5 arguments, we can partially apply 3 of
them. But with currying, we only pass one argument at a time.
So if a function takes 5 arguments, we have to curry 5 times
before we get the resulting value.
Future and Promise are an application of currying.
Currying

More Related Content

PDF
Functional programming java
PPT
C++ Function
PPTX
Function in c++
PPT
Functions in C++
PPTX
parameter passing in c#
PPTX
Working with functions in matlab
PPTX
Function Parameters
PPTX
User defined Functions in MATLAB Part 1
Functional programming java
C++ Function
Function in c++
Functions in C++
parameter passing in c#
Working with functions in matlab
Function Parameters
User defined Functions in MATLAB Part 1

What's hot (19)

PPT
FUNCTIONS IN c++ PPT
PDF
Functional Programming in JavaScript
PPT
Functions in C++
PPTX
Call by value
PPTX
Parameter passing to_functions_in_c
PPTX
functions of C++
PPT
Lecture#6 functions in c++
PPT
16717 functions in C++
 
PPTX
Functions in C++
PPTX
Call by value or call by reference in C++
PPT
Function overloading(C++)
PPTX
Function C++
PPTX
C++ programming function
PPTX
Inline function
PDF
03 function overloading
PPT
CPP Language Basics - Reference
PPT
Functions in C++
PPTX
PARAMETER PASSING MECHANISMS
PPTX
INLINE FUNCTION IN C++
FUNCTIONS IN c++ PPT
Functional Programming in JavaScript
Functions in C++
Call by value
Parameter passing to_functions_in_c
functions of C++
Lecture#6 functions in c++
16717 functions in C++
 
Functions in C++
Call by value or call by reference in C++
Function overloading(C++)
Function C++
C++ programming function
Inline function
03 function overloading
CPP Language Basics - Reference
Functions in C++
PARAMETER PASSING MECHANISMS
INLINE FUNCTION IN C++
Ad

Similar to Functional programming 101 (20)

PDF
From object oriented to functional domain modeling
ODP
From object oriented to functional domain modeling
PDF
PPTX
Functions
PPTX
Python Lecture 4
PPTX
Functionincprogram
PDF
Functional Programming with Javascript
PPTX
Functional Programming in Swift
PPT
Lecture 11 - Functions
PPT
Generalized Functors - Realizing Command Design Pattern in C++
PDF
Functions
ODP
Functional programming
PDF
Basics of Functional Programming
PPTX
Function in C++, Methods in C++ coding programming
PPTX
Function in c
PPT
PPT
An Overview Of Python With Functional Programming
PPT
Functional Programming
PPT
Material 3 (4).ppt this ppt is about the
PPT
Basic information of function in cpu
From object oriented to functional domain modeling
From object oriented to functional domain modeling
Functions
Python Lecture 4
Functionincprogram
Functional Programming with Javascript
Functional Programming in Swift
Lecture 11 - Functions
Generalized Functors - Realizing Command Design Pattern in C++
Functions
Functional programming
Basics of Functional Programming
Function in C++, Methods in C++ coding programming
Function in c
An Overview Of Python With Functional Programming
Functional Programming
Material 3 (4).ppt this ppt is about the
Basic information of function in cpu
Ad

Recently uploaded (20)

PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
Transform Your Business with a Software ERP System
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Digital Strategies for Manufacturing Companies
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
medical staffing services at VALiNTRY
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
ai tools demonstartion for schools and inter college
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
2025 Textile ERP Trends: SAP, Odoo & Oracle
Transform Your Business with a Software ERP System
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Digital Strategies for Manufacturing Companies
How Creative Agencies Leverage Project Management Software.pdf
VVF-Customer-Presentation2025-Ver1.9.pptx
How to Choose the Right IT Partner for Your Business in Malaysia
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
medical staffing services at VALiNTRY
Odoo Companies in India – Driving Business Transformation.pdf
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
CHAPTER 2 - PM Management and IT Context
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
ai tools demonstartion for schools and inter college
PTS Company Brochure 2025 (1).pdf.......
Which alternative to Crystal Reports is best for small or large businesses.pdf

Functional programming 101

  • 2. Agenda What is FP Basics of FP Pure Functions Immutability Referential Transparency Higher Order Functions Partial Application and Currying
  • 3. What is FP ? In functional programming, we place a major emphasis on writing code using functions as building blocks. Your program is defined in terms of one main function. This main function is defined in terms of other functions, which are in turn defined in terms of still more functions, until at the bottom level the functions are just language primitives like number or string.
  • 5. Basics Every Software program has two things ● Behavior(What the program does) ● Data In FP , behavior is handled only using functions and data is immutable. Rather than changing data they take in, functions in functional programming take in data as input and produce new values as output(always). Also functions in FP can be passed around just like data.
  • 7. Functions ● Functions are pure ● Functions use immutable data ● Functions guarantee referential transparency ● Functions are first-class entities
  • 8. Pure Functions Qualities of Pure Functions ● The function depends only on the input provided to it to produce a result (and not on any external state). ● The function does not cause any observable side effects, such as modifying a global object or modifying a parameter passed by reference
  • 9. Pure Functions var PI = 3.14; const calculateArea = (radius) => radius * radius * PI; let count = 1; const increment = (val) => count = count + val; const reverseArray = (array) => array.reverse();
  • 10. Pure Functions async function amountExceedsCreditLimit(amount) { const limit = await getCreditLimit() // assume api call if (amount > limit) { return true; } return false; } console.log(“Hello World!”);
  • 11. Pure Functions benefits ● Pure functions are predictable ● Pure functions are easy to test ● Easier to refactor
  • 12. Immutability var firstName = “Maneesh” firstName[3] = “i”; console.log(firstName[3]); console.log(firstName); firstName = firstName.slice(0,3); console.log(firstName);
  • 13. Immutability Mutation modifies the underlying object without affecting the link between the variable and the object, whereas assignment changes the link to a different object, but doesn’t modify any objects.
  • 15. Functions -Referential Transparency If a function consistently yields the same result for the same input , it is referentially transparent. Referential transparency gives the ability to freely replace an expression with its value and not change the behavior of the program.
  • 16. Functions -Referential Transparency Pure functions + immutable data = referential transparency i.e a function that doesn't rely on external states or mutable data will always return the same output for a given input.
  • 17. Functions are first class Citizens ● Refer to it from constants and variables ● Pass it as a parameter to other functions ● Return it as result from other functions
  • 18. A higher order function is a function that meets at least one of the following requirements: Note most functions which take a callback are higher order functions. ● It accepts a function as an argument ● It returns a new function Higher Order Functions
  • 19. Benefits of higher order functions ● Hide Implementation Details numbers.stream().map(s -> Integer.valueOf(s)) .filter(number -> number % 2 == 0) .collect(Collectors.toList()); Higher Order Functions
  • 20. Benefits of higher order functions ● Adding Functionality to an Existing Function function logResult(fn) { return (...args) => { const result = fn(...args) console.log(result) return result }} Higher Order Functions
  • 21. const findAndMultiply = (index,multiplier) => { return (array) => { if (array && typeof(array[index] == ‘number’) { return array[index] * multiplier ; else{ Return “Bhaiya, ye na ho pai”’ } }; }; Higher Order Functions
  • 23. Create small reusable functions that you can combine to compose new functions. BiFunction<String, List<Article>, List<Article>> byAuthor = (name, articles) -> articles.stream() .filter(a -> a.getAuthor().equals(name)) .collect(Collectors.toList()); Function Composition
  • 24. BiFunction<String, List<Article>, List<Article>> byTag = (tag, articles) -> articles.stream() .filter(a -> a.getTags().contains(tag)) .collect(Collectors.toList()); Function<List<Article>, List<Article>> sortByDate = articles -> articles.stream() .sorted((x, y) -> y.published().compareTo(x.published())) .collect(Collectors.toList()); Function Composition
  • 25. Function<List<Article>, Optional<Article>> first = a -> a.stream().findFirst(); Function<List<Article>, Optional<Article>> newest = first.compose(sortByDate); BiFunction<String, List<Article>, List<Article>> byAuthorSorted = byAuthor.andThen(sortByDate); Function Composition
  • 26. The compose method uses a parameter named, before, and the andThen method uses a parameter named, after. These names indicate the order that the functions will be evaluated on. f(x) = (2+x)*3 g(x) = 2 +(x*3) Use compose and andThen to form f(x) and g(x). Function Composition
  • 27. Function<Integer, Integer> baseFunction = t -> t + 2; Function<Integer, Integer> afterFunction = baseFunction.andThen(t -> t * 3); System.out.println(afterFunction.apply(5)); Function<Integer, Integer> beforeFunction = baseFunction.compose(t -> t * 3); System.out.println(beforeFunction.apply(5)); when the apply method is executed, the corresponding lambda expression is executed Function Composition
  • 28. int hoursWorked[] = {8, 12, 8, 6, 6, 5, 6, 0}; int totalHoursWorked = Arrays.stream(hoursWorked) .filter(n -> n > 6) .sum(); LocalDateTime timeInstance = LocalDateTime.now() .plusDays(3) .minusHours(4) .plusWeeks(1) .plusYears(2); Fluent Interfaces
  • 29. Partial application is a way to turn a function that expects multiple parameters into one that will keep returning a new function until it receives all its arguments const f = (a,b,c) => a * b *c const g = partial(f,[2,3]) const h = g(4) Partial Application
  • 30. const applyTax = (amount,tax) => amount + (amount * tax) applyTax(0.08) => applyTaxOfEightPercent = partial(applyTax,[0.08]); applyTaxOfFivePercent = partial(applyTax,[0.05]); applyTaxofEightPercent(100); applyTaxofFivePercent(100); Exercise - Template design pattern. Try implementing it using partial application. Partial Application
  • 31. Currying is the process of taking a function that accepts N arguments and turning it into a chained series of N functions that each take one argument. The difference between partial application and currying is that with currying, you can never provide more than one argument — it has to be either one or zero. If a function takes 5 arguments, we can partially apply 3 of them. But with currying, we only pass one argument at a time. So if a function takes 5 arguments, we have to curry 5 times before we get the resulting value. Future and Promise are an application of currying. Currying