24
FUNCTIONAL PROGRAMMING IN A NUTSHELL Adityo Pratomo (Framework) TiA PDC’17

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

Embed Size (px)

Citation preview

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

FUNCTIONAL PROGRAMMING IN A NUTSHELL

Adityo Pratomo (Framework)TiA PDC’17

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

HELLO, I’M DIDIT

Chief Academic OfficerFroyo Framework

Jakarta-based IT trainer provider

CTO & Production ManagerLabtek Indie

Bandung-based Digital Product R&D Company

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

HELLO, I’M DIDIT

I mainly developInteractive graphics,

game, hardware

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

OVERVIEW

What is functional programming?

Functional programming vs imperative programming

Building block of functional programming

How functional programming will help you?

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

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)

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

TOOLS FOR PROGRAMMERS

Software

Frameworks

Programming Language

Programming Paradigm

Functional Programming

Page 7: "Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)
Page 8: "Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)

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

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

FUNCTIONAL VS IMPERATIVE: AN ANALOGY

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

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

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

MI REBUS: THE FUNCTIONAL WAY

1. Mi rebus components:i. Boiling waterii. Cooked noodle and seasoningsiii. Served noodle on bowl

2. Compositions of components:i. Serve(cooked(boiled(noodle and seasonings)))

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

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 waterii. Cooked noodle and seasoningsiii. Served noodle on bowl

2. Compositions of components:i. Serve(cooked(boiled(noodle and

seasonings)))

Imperative:1. Focuses on HOW to do things2. A set of sequential instructions3. Depends on state to operate4. Not necessarily reusable, each new

product might require using set of new instructions

Functional:1. Focuses on WHAT is a thing2. Composing set of functions, each

functions has a clear result3. Not depends on state4. Each functions can be reused by

including in different compositions

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

FUNCTIONAL VS IMPERATIVE

Menghitung nilai terbesar dan rata-rata dari sebuah data

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

FUNCTIONAL VS IMPERATIVE

Menghitung nilai terbesar dan rata-rata dari sebuah data

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

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

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

FUNCTIONAL VS IMPERATIVE

Normalize the numbers by:

- Count average

- Increase numbers smaller than average

- Decrease numbers smaller by average

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

BUILDING BLOCKS OF FUNCTIONAL PROGRAMMING1. 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;}

}

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

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));

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

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 }],

Page 20: "Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)
Page 21: "Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)

HOW FUNCTIONAL PROGRAMMING HELPS?

Pure functions

Immutable Data

Create testable software from the ground up

Reduce bugs Create multithread application

Create true modular software

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

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)

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

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

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

MAKE SOMETHING AWESOME!

Thank [email protected]

[email protected]@kotakmakan