Sunteți pe pagina 1din 24

FUNCTIONAL PROGRAMMING IN A Adityo Pratomo (Framework)

NUTSHELL TiA PDC17


HELLO, IM DIDIT

Chief Academic Officer CTO & Production Manager


Froyo Framework Labtek Indie
Jakarta-based IT trainer provider Bandung-based Digital Product R&D
Company
HELLO, IM 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
Thoughts Codes into
into codes instructions
Programmers are required
to instruct machine and
communicate to humans

programmer source code WHILE solving problems


computer
according to set of rules
(languages, frameworks,
Read code
hardware architecture, and
for analysis
so forth)

Co-workers
TOOLS FOR PROGRAMMERS
Software

Frameworks

Programming Language
Functional Programming
Programming Paradigm
Functional programming is a programming paradigma style of
building the structure and elements of computer programsthat
treats computation as the evaluation of mathematical functions and avoids
data.
changing-state and mutable data

It is a declarative programming paradigm, which means programming is


done with expressions or declarations instead of statements.
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 1. Mi rebus components:
2. Turn on stove to boil the water i. Boiling water
Functional: ii. Imperative:
Cooked noodle and seasonings
3. After the water
1. Focuses boils,
on WHAT is a insert
thing the iii. 1.Served
Focusesnoodle
on HOWon
to bowl
do things
noodle
2. Composing set of functions, each 2. A set of sequential instructions
4. Add
functions has a clear result
seasonings 2. Compositions
3. Depends onof components:
state to operate
3. Not depends on state i. 4.Serve(cooked(boiled(noodle
Not necessarily reusable, each new
and
4. Each
5. Cook forfunctions can be reused by
3 minutes product might require using set of new
seasonings)))
including in different compositions instructions
6. Pour noodle from the saucepan along
with the soup to a bowl
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-


Functions are being used to describe a
by step instructions of doing things
desired result from an operation
Relies on for loop
Relies on array method
Involves temporary mutable variable to store
No mutable variable and no state
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 const myData = [51, 72, 38, 94];
A data whose value(s) cant be change function isEven (x) {
2. Pure function if (x % 2 == 0) {
A function whose return value is only return true;
determined by its input values, without }
observable side effects }
Produce the same output when
processing the same input
PURE FUNCTIONS AND FUNCTION COMPOSITION
Always returns a function or value function compose (f, g) {
return function (x) {
A reusable building block of a program return f(g(x));
}
Several functions can be composed to do }

data processing, creating a new function function add2(x) {


return x + 2;
f(x) => y }

g(x) => z function multiply4(x) {


return x*4;
f o g (x) = f(g(x)) }

var add2Multiply4 = compose(add2, multiply4);


console.log(add2Multiply4(2));
PURE FUNCTIONS AND FUNCTION COMPOSITION
const albumList = [ const getMetallica = (arr) => arr.filter((item) =>
{ item.artist === 'Metallica');
artist: "Metallica",
title: "Master of Puppets", const getAlbumIn1990 = (arr) => arr.filter((item) =>
year: 1986 item.year === 1990);
},
{
artist: "Metallica", const getMetallicaAlbumIn1990 =
title: "Black Album", compose(getMetallica, getAlbumIn1990);
year: 1990 console.log(getMetallicaAlbumIn1990(albumList));
},
{ //[ { artist: "Metallica", title: "Black Album", year: 1990 }],
artist: "Megadeth",
title: "Rust in Peace",
year: 1990
}
]
HOW FUNCTIONAL PROGRAMMING HELPS?
Pure functions

Create testable software Reduce bugs Create multithread Create true modular
from the ground up application software

Immutable Data
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 doesnt 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

S-ar putea să vă placă și