SlideShare ist ein Scribd-Unternehmen logo
1 von 24
Downloaden Sie, um offline zu lesen
FUNCTIONAL PROGRAMMING IN A
NUTSHELL
Adityo Pratomo (Framework)
TiA PDC’17
HELLO, I’M DIDIT
Chief Academic Officer
Froyo Framework
Jakarta-based IT trainer provider
CTO & Production Manager
Labtek Indie
Bandung-based Digital Product R&D
Company
HELLO, I’M DIDIT
I mainly develop
Interactive graphics,
game, hardware
OVERVIEW
What is functional programming?
Functional programming vs imperative programming
Building block of functional programming
How functional programming will help you?
PROGRAMMING
programmer source code computer
Co-workers
Thoughts
into codes
Codes into
instructions
Read code
for analysis
Programmers are required
to instruct machine and
communicate to humans
WHILE solving problems
according to set of rules
(languages, frameworks,
hardware architecture, and
so forth)
TOOLS FOR PROGRAMMERS
Software
Frameworks
Programming Language
Programming Paradigm
Functional Programming
Functional programming is a programming paradigm—a style of
building the structure and elements of computer programs—that
treats computation as the evaluation of mathematical functions and avoids
changing-state and mutable data.
It is a declarative programming paradigm, which means programming is
done with expressions or declarations instead of statements.
treats computation as the evaluation of mathematical functions and avoids
changing-state and mutable data
FUNCTIONAL VS IMPERATIVE: AN ANALOGY
MI REBUS: THE IMPERATIVE WAY
1. Pour water into saucepan
2. Turn on stove to boil the water
3. After the water boils, insert the noodle
4. Add seasonings
5. Cook for 3 minutes
6. Pour noodle from the saucepan along with the soup to a bowl
MI REBUS: THE FUNCTIONAL WAY
1. Mi rebus components:
i. Boiling water
ii. Cooked noodle and seasonings
iii. Served noodle on bowl
2. Compositions of components:
i. Serve(cooked(boiled(noodle and seasonings)))
FUNCTIONAL VS IMPERATIVE
1. Pour water into saucepan
2. Turn on stove to boil the water
3. After the water boils, insert the
noodle
4. Add seasonings
5. Cook for 3 minutes
6. Pour noodle from the saucepan along
with the soup to a bowl
1. Mi rebus components:
i. Boiling water
ii. Cooked noodle and seasonings
iii. Served noodle on bowl
2. Compositions of components:
i. Serve(cooked(boiled(noodle and
seasonings)))
Imperative:
1. Focuses on HOW to do things
2. A set of sequential instructions
3. Depends on state to operate
4. Not necessarily reusable, each new
product might require using set of new
instructions
Functional:
1. Focuses on WHAT is a thing
2. Composing set of functions, each
functions has a clear result
3. Not depends on state
4. Each functions can be reused by
including in different compositions
FUNCTIONAL VS IMPERATIVE
Menghitung nilai terbesar dan rata-rata
dari sebuah data
FUNCTIONAL VS IMPERATIVE
Menghitung nilai terbesar dan rata-rata
dari sebuah data
FUNCTIONAL VS IMPERATIVE
• Functions are being used to describe a step-
by step instructions of doing things
• Relies on for loop
• Involves temporary mutable variable to store
state
• Functions are being used to describe a
desired result from an operation
• Relies on array method
• No mutable variable and no state
FUNCTIONAL VS IMPERATIVE
Normalize the numbers by:
- Count average
- Increase numbers smaller than average
- Decrease numbers smaller by average
BUILDING BLOCKS OF FUNCTIONAL
PROGRAMMING
1. Immutable data
­ A data whose value(s) can’t be change
2. Pure function
­ A function whose return value is only
determined by its input values, without
observable side effects
­ Produce the same output when
processing the same input
const myData = [51, 72, 38, 94];
function isEven (x) {
if (x % 2 == 0) {
return true;
}
}
PURE FUNCTIONS AND FUNCTION COMPOSITION
Always returns a function or value
A reusable building block of a program
Several functions can be composed to do
data processing, creating a new function
f(x) => y
g(x) => z
f o g (x) = f(g(x))
function compose (f, g) {
return function (x) {
return f(g(x));
}
}
function add2(x) {
return x + 2;
}
function multiply4(x) {
return x*4;
}
var add2Multiply4 = compose(add2, multiply4);
console.log(add2Multiply4(2));
PURE FUNCTIONS AND FUNCTION COMPOSITION
const albumList = [
{
artist: "Metallica",
title: "Master of Puppets",
year: 1986
},
{
artist: "Metallica",
title: "Black Album",
year: 1990
},
{
artist: "Megadeth",
title: "Rust in Peace",
year: 1990
}
]
const getMetallica = (arr) => arr.filter((item) =>
item.artist === 'Metallica');
const getAlbumIn1990 = (arr) => arr.filter((item) =>
item.year === 1990);
const getMetallicaAlbumIn1990 =
compose(getMetallica, getAlbumIn1990);
console.log(getMetallicaAlbumIn1990(albumList));
//[ { artist: "Metallica", title: "Black Album", year: 1990 }],
HOW FUNCTIONAL PROGRAMMING HELPS?
Pure functions
Immutable Data
Create testable software
from the ground up
Reduce bugs Create multithread
application
Create true modular
software
WHERE CAN I USE IT?
-Back end:
- Various data processing and simulation (Scala, Haskell, Clojure, Elixir, Erlang, etc.)
-Front end:
- One way data rendering (Elm)
-Anywhere:
- Code in your favourite language using functional programming style (C#, C++, JavaScript, Python)
LAST NOTE
Pure functional programming have no side effects
­ Real world application relies on side effects for I/O operation
­ Use functional style to manage the side effects
Functional programming doesn’t use state
­ Game programing relies on state to manage the game (level, progressions, health, etc)
­ Use state to manage, but inner operations can still use FP
MAKE SOMETHING AWESOME!
Thank You
didit@froyo.co.id
didit@labtekindie.com
@kotakmakan

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (11)

Recursion in c++
Recursion in c++Recursion in c++
Recursion in c++
 
Programming meeting #3
Programming meeting #3Programming meeting #3
Programming meeting #3
 
Code craftsmanship saturdays second session
Code craftsmanship saturdays second sessionCode craftsmanship saturdays second session
Code craftsmanship saturdays second session
 
Curry functions in Javascript
Curry functions in JavascriptCurry functions in Javascript
Curry functions in Javascript
 
Passing Parameters using File and Command Line
Passing Parameters using File and Command LinePassing Parameters using File and Command Line
Passing Parameters using File and Command Line
 
Actor, an elegant model for concurrent and distributed computation
Actor, an elegant model for concurrent and distributed computationActor, an elegant model for concurrent and distributed computation
Actor, an elegant model for concurrent and distributed computation
 
Removal Of Recursion
Removal Of RecursionRemoval Of Recursion
Removal Of Recursion
 
functions
functionsfunctions
functions
 
Write a program that initializes an array - of - double and then copies the c...
Write a program that initializes an array - of - double and then copies the c...Write a program that initializes an array - of - double and then copies the c...
Write a program that initializes an array - of - double and then copies the c...
 
Function composition in Javascript
Function composition in JavascriptFunction composition in Javascript
Function composition in Javascript
 
Javascript Function
Javascript FunctionJavascript Function
Javascript Function
 

Ähnlich wie "Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)

C++ 2
C++ 2C++ 2
C++ 2
jani
 

Ähnlich wie "Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework) (20)

Functional programming 101
Functional programming 101Functional programming 101
Functional programming 101
 
User defined functions.1
User defined functions.1User defined functions.1
User defined functions.1
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpu
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional Programming
 
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
 
Nikolai Boiko "NodeJS Refactoring: How to kill a Dragon and stay alive"
Nikolai Boiko "NodeJS Refactoring: How to kill a Dragon and stay alive"Nikolai Boiko "NodeJS Refactoring: How to kill a Dragon and stay alive"
Nikolai Boiko "NodeJS Refactoring: How to kill a Dragon and stay alive"
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
C++ 2
C++ 2C++ 2
C++ 2
 
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
 
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
 
Integration-Monday-Stateful-Programming-Models-Serverless-Functions
Integration-Monday-Stateful-Programming-Models-Serverless-FunctionsIntegration-Monday-Stateful-Programming-Models-Serverless-Functions
Integration-Monday-Stateful-Programming-Models-Serverless-Functions
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
 
Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++
 
Python functional programming
Python functional programmingPython functional programming
Python functional programming
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Function
Function Function
Function
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
C function presentation
C function presentationC function presentation
C function presentation
 

Mehr von Tech in Asia ID

Mehr von Tech in Asia ID (20)

Sesi Tech in Asia PDC'21.pdf
Sesi Tech in Asia PDC'21.pdfSesi Tech in Asia PDC'21.pdf
Sesi Tech in Asia PDC'21.pdf
 
"ILO's Work on Skills Development" by Project Coordinators International Labo...
"ILO's Work on Skills Development" by Project Coordinators International Labo..."ILO's Work on Skills Development" by Project Coordinators International Labo...
"ILO's Work on Skills Development" by Project Coordinators International Labo...
 
"Women in STEM: Leveraging Talent in ICT Sector" by Maya Juwita (Executive Di...
"Women in STEM: Leveraging Talent in ICT Sector" by Maya Juwita (Executive Di..."Women in STEM: Leveraging Talent in ICT Sector" by Maya Juwita (Executive Di...
"Women in STEM: Leveraging Talent in ICT Sector" by Maya Juwita (Executive Di...
 
Laporan Kondisi Pendanaan Startup di Indonesia Kuartal Ketiga Tahun 2018
Laporan Kondisi Pendanaan Startup di Indonesia Kuartal Ketiga Tahun 2018Laporan Kondisi Pendanaan Startup di Indonesia Kuartal Ketiga Tahun 2018
Laporan Kondisi Pendanaan Startup di Indonesia Kuartal Ketiga Tahun 2018
 
LinkedIn Pitch Deck
LinkedIn Pitch DeckLinkedIn Pitch Deck
LinkedIn Pitch Deck
 
Laporan Kondisi Pendanaan Startup di Indonesia Kuartal Kedua Tahun 2018
Laporan Kondisi Pendanaan Startup di Indonesia Kuartal Kedua Tahun 2018Laporan Kondisi Pendanaan Startup di Indonesia Kuartal Kedua Tahun 2018
Laporan Kondisi Pendanaan Startup di Indonesia Kuartal Kedua Tahun 2018
 
Laporan Kondisi Pendanaan Startup di Indonesia Kuartal Pertama Tahun 2018
Laporan Kondisi Pendanaan Startup di Indonesia Kuartal Pertama Tahun 2018Laporan Kondisi Pendanaan Startup di Indonesia Kuartal Pertama Tahun 2018
Laporan Kondisi Pendanaan Startup di Indonesia Kuartal Pertama Tahun 2018
 
Laporan Kondisi Pendanaan Startup di Indonesia Tahun 2017
Laporan Kondisi Pendanaan Startup di Indonesia Tahun 2017Laporan Kondisi Pendanaan Startup di Indonesia Tahun 2017
Laporan Kondisi Pendanaan Startup di Indonesia Tahun 2017
 
"Less Painful iOS Development" by Samuel Edwin (Tokopedia)
"Less Painful iOS Development" by Samuel Edwin (Tokopedia)"Less Painful iOS Development" by Samuel Edwin (Tokopedia)
"Less Painful iOS Development" by Samuel Edwin (Tokopedia)
 
"Product Development Story Loket.com" by Aruna Laksana (Loket.com)
"Product Development Story Loket.com" by Aruna Laksana (Loket.com)"Product Development Story Loket.com" by Aruna Laksana (Loket.com)
"Product Development Story Loket.com" by Aruna Laksana (Loket.com)
 
"Making Data Actionable" by Budiman Rusly (KMK Online)
"Making Data Actionable" by Budiman Rusly (KMK Online)"Making Data Actionable" by Budiman Rusly (KMK Online)
"Making Data Actionable" by Budiman Rusly (KMK Online)
 
"DOKU under the hood : Infrastructure and Cloud Services Technology" by M. T...
"DOKU under the hood :  Infrastructure and Cloud Services Technology" by M. T..."DOKU under the hood :  Infrastructure and Cloud Services Technology" by M. T...
"DOKU under the hood : Infrastructure and Cloud Services Technology" by M. T...
 
Citcall : Real-Time User Verification with Missed-Call Based OTP
Citcall : Real-Time User Verification with Missed-Call Based OTPCitcall : Real-Time User Verification with Missed-Call Based OTP
Citcall : Real-Time User Verification with Missed-Call Based OTP
 
"Building High Performance Search Feature" by Setyo Legowo (UrbanIndo)
"Building High Performance Search Feature" by Setyo Legowo (UrbanIndo)"Building High Performance Search Feature" by Setyo Legowo (UrbanIndo)
"Building High Performance Search Feature" by Setyo Legowo (UrbanIndo)
 
"Building Effective Developer-Designer Relationships" by Ifnu Bima (Blibli.com)
"Building Effective Developer-Designer Relationships" by Ifnu Bima (Blibli.com)"Building Effective Developer-Designer Relationships" by Ifnu Bima (Blibli.com)
"Building Effective Developer-Designer Relationships" by Ifnu Bima (Blibli.com)
 
"Data Informed vs Data Driven" by Casper Sermsuksan (Kulina)
"Data Informed vs Data Driven" by Casper Sermsuksan (Kulina)"Data Informed vs Data Driven" by Casper Sermsuksan (Kulina)
"Data Informed vs Data Driven" by Casper Sermsuksan (Kulina)
 
"Planning Your Analytics Implementation" by Bachtiar Rifai (Kofera Technology)
"Planning Your Analytics Implementation" by Bachtiar Rifai (Kofera Technology)"Planning Your Analytics Implementation" by Bachtiar Rifai (Kofera Technology)
"Planning Your Analytics Implementation" by Bachtiar Rifai (Kofera Technology)
 
"How To Build and Lead a Winning Data Team" by Cahyo Listyanto (Bizzy.co.id)
"How To Build and Lead a Winning Data Team" by Cahyo Listyanto (Bizzy.co.id)"How To Build and Lead a Winning Data Team" by Cahyo Listyanto (Bizzy.co.id)
"How To Build and Lead a Winning Data Team" by Cahyo Listyanto (Bizzy.co.id)
 
"How Scrum Motivates People" by Rudy Rahadian (XL Axiata)
"How Scrum Motivates People" by Rudy Rahadian (XL Axiata)"How Scrum Motivates People" by Rudy Rahadian (XL Axiata)
"How Scrum Motivates People" by Rudy Rahadian (XL Axiata)
 
"Control Your Mobile Development Cost" by Ray Rizaldy (GITS.ID)
"Control Your Mobile Development Cost" by Ray Rizaldy  (GITS.ID)"Control Your Mobile Development Cost" by Ray Rizaldy  (GITS.ID)
"Control Your Mobile Development Cost" by Ray Rizaldy (GITS.ID)
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

"Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)

  • 1. FUNCTIONAL PROGRAMMING IN A NUTSHELL Adityo Pratomo (Framework) TiA PDC’17
  • 2. HELLO, I’M DIDIT Chief Academic Officer Froyo Framework Jakarta-based IT trainer provider CTO & Production Manager Labtek Indie Bandung-based Digital Product R&D Company
  • 3. HELLO, I’M DIDIT I mainly develop Interactive graphics, game, hardware
  • 4. OVERVIEW What is functional programming? Functional programming vs imperative programming Building block of functional programming How functional programming will help you?
  • 5. PROGRAMMING programmer source code computer Co-workers Thoughts into codes Codes into instructions Read code for analysis Programmers are required to instruct machine and communicate to humans WHILE solving problems according to set of rules (languages, frameworks, hardware architecture, and so forth)
  • 6. TOOLS FOR PROGRAMMERS Software Frameworks Programming Language Programming Paradigm Functional Programming
  • 7.
  • 8. Functional programming is a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. It is a declarative programming paradigm, which means programming is done with expressions or declarations instead of statements. treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data
  • 10. MI REBUS: THE IMPERATIVE WAY 1. Pour water into saucepan 2. Turn on stove to boil the water 3. After the water boils, insert the noodle 4. Add seasonings 5. Cook for 3 minutes 6. Pour noodle from the saucepan along with the soup to a bowl
  • 11. MI REBUS: THE FUNCTIONAL WAY 1. Mi rebus components: i. Boiling water ii. Cooked noodle and seasonings iii. Served noodle on bowl 2. Compositions of components: i. Serve(cooked(boiled(noodle and seasonings)))
  • 12. FUNCTIONAL VS IMPERATIVE 1. Pour water into saucepan 2. Turn on stove to boil the water 3. After the water boils, insert the noodle 4. Add seasonings 5. Cook for 3 minutes 6. Pour noodle from the saucepan along with the soup to a bowl 1. Mi rebus components: i. Boiling water ii. Cooked noodle and seasonings iii. Served noodle on bowl 2. Compositions of components: i. Serve(cooked(boiled(noodle and seasonings))) Imperative: 1. Focuses on HOW to do things 2. A set of sequential instructions 3. Depends on state to operate 4. Not necessarily reusable, each new product might require using set of new instructions Functional: 1. Focuses on WHAT is a thing 2. Composing set of functions, each functions has a clear result 3. Not depends on state 4. Each functions can be reused by including in different compositions
  • 13. FUNCTIONAL VS IMPERATIVE Menghitung nilai terbesar dan rata-rata dari sebuah data
  • 14. FUNCTIONAL VS IMPERATIVE Menghitung nilai terbesar dan rata-rata dari sebuah data
  • 15. FUNCTIONAL VS IMPERATIVE • Functions are being used to describe a step- by step instructions of doing things • Relies on for loop • Involves temporary mutable variable to store state • Functions are being used to describe a desired result from an operation • Relies on array method • No mutable variable and no state
  • 16. FUNCTIONAL VS IMPERATIVE Normalize the numbers by: - Count average - Increase numbers smaller than average - Decrease numbers smaller by average
  • 17. BUILDING BLOCKS OF FUNCTIONAL PROGRAMMING 1. Immutable data ­ A data whose value(s) can’t be change 2. Pure function ­ A function whose return value is only determined by its input values, without observable side effects ­ Produce the same output when processing the same input const myData = [51, 72, 38, 94]; function isEven (x) { if (x % 2 == 0) { return true; } }
  • 18. PURE FUNCTIONS AND FUNCTION COMPOSITION Always returns a function or value A reusable building block of a program Several functions can be composed to do data processing, creating a new function f(x) => y g(x) => z f o g (x) = f(g(x)) function compose (f, g) { return function (x) { return f(g(x)); } } function add2(x) { return x + 2; } function multiply4(x) { return x*4; } var add2Multiply4 = compose(add2, multiply4); console.log(add2Multiply4(2));
  • 19. PURE FUNCTIONS AND FUNCTION COMPOSITION const albumList = [ { artist: "Metallica", title: "Master of Puppets", year: 1986 }, { artist: "Metallica", title: "Black Album", year: 1990 }, { artist: "Megadeth", title: "Rust in Peace", year: 1990 } ] const getMetallica = (arr) => arr.filter((item) => item.artist === 'Metallica'); const getAlbumIn1990 = (arr) => arr.filter((item) => item.year === 1990); const getMetallicaAlbumIn1990 = compose(getMetallica, getAlbumIn1990); console.log(getMetallicaAlbumIn1990(albumList)); //[ { artist: "Metallica", title: "Black Album", year: 1990 }],
  • 20.
  • 21. HOW FUNCTIONAL PROGRAMMING HELPS? Pure functions Immutable Data Create testable software from the ground up Reduce bugs Create multithread application Create true modular software
  • 22. WHERE CAN I USE IT? -Back end: - Various data processing and simulation (Scala, Haskell, Clojure, Elixir, Erlang, etc.) -Front end: - One way data rendering (Elm) -Anywhere: - Code in your favourite language using functional programming style (C#, C++, JavaScript, Python)
  • 23. LAST NOTE Pure functional programming have no side effects ­ Real world application relies on side effects for I/O operation ­ Use functional style to manage the side effects Functional programming doesn’t use state ­ Game programing relies on state to manage the game (level, progressions, health, etc) ­ Use state to manage, but inner operations can still use FP
  • 24. MAKE SOMETHING AWESOME! Thank You didit@froyo.co.id didit@labtekindie.com @kotakmakan