Slide 1

Slide 1 text

Functional Programming SASTRA NABABAN @sastranababan

Slide 2

Slide 2 text

–Eric Elliott 
 Author of Programming Javascript Applications “JavaScript is one of the most important programming languages of all time, not simply because of its popularity, but because it popularized two features which are extremely important for the evolution of programming: Prototypal Inheritance & Functional Programming ”

Slide 3

Slide 3 text

What is Functional Programming ? functional programming is a programming paradigm. a style of building the structure and elements of computer programs paradigm, style, mindset, the way of thinking, technique Keywords :

Slide 4

Slide 4 text

History First introduced in the 1930s by mathematician Alonzo Church λ (lambda) calculus as a basis for functional programming. Alonzo Church + American Mathematician (June 14, 1903 – August 11, 1995)

Slide 5

Slide 5 text

List of Major Programming Paradigms • Imperative / Procedural • Declarative • Object Oriented • Functional http://cs.lmu.edu/~ray/notes/paradigms/

Slide 6

Slide 6 text

Why is Functional Programming ? Less Bugs Less Time, re-use more code More readeable code More performance

Slide 7

Slide 7 text

“ do everything in function “

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Functional Programming Concept ๏ Declarative ๏ Pure Functions ๏ No Looping ๏ First-class objects ๏ High-order Functions ๏ Map, Reduce, Filter

Slide 10

Slide 10 text

Imperative vs Declarative How What Characteristic Imperative Declarative Programming Style Step by Step Define Problem & Transformation State Change Important Non-existent Order Of Execution Important Not as Important Flow Control Loop, Conditional Recursion

Slide 11

Slide 11 text

Example #1 double all the numbers in an array Declarative Imperative

Slide 12

Slide 12 text

Example #2 SQL query is declarative, what if write on imperative style ? Imperative Declarative

Slide 13

Slide 13 text

“ do everything in function I mean pure function “

Slide 14

Slide 14 text

Pure Function A pure function doesn’t depend on and doesn’t modify the states of variables out of its scope. Pilar of Functional Programming

Slide 15

Slide 15 text

• Always have input and return output ( value ) • No side effects • Given the same input, will always return the same output. • Relies on no external mutable state. Rule of Pure Function : impure function : function add(x,y){ var z = x + y } pure function : function add(x,y){ return x + y }

Slide 16

Slide 16 text

Impure Function Pure Function

Slide 17

Slide 17 text

“ No Loop in Functional Programing “

Slide 18

Slide 18 text

Use recursion baby !!!!

Slide 19

Slide 19 text

Let's see how it works

Slide 20

Slide 20 text

Impure Function Pure Function

Slide 21

Slide 21 text

First-class objects In JavaScript functions are first-class. This mean function can : • store the function in a variable • have properties • pass the function as a parameter to another function • return the function from a function

Slide 22

Slide 22 text

Store Function in Variable Have Properties & Method

Slide 23

Slide 23 text

High-order Function Function that operates on other functions. Takes functions as inputs and/or returns them as outputs. Function that create and return new function : When to use High-Order Function : • Asynchronous execution (Node.js, ajax) • Event Listeners/Handlers (jquery dll) • setTimeout and setInterval methods

Slide 24

Slide 24 text

Map, Filter, Reduce Map Filter Reduce transform each of them Filter out certain item compute to new value we never manipulate the original array

Slide 25

Slide 25 text

Map array.map(function(elem, index, array) { ... } Example: convert Fahrenheit temps to Celsius. var fahrenheit = [0, 32, 45, 50, 75, 80, 99, 120]; var celcius = fahrenheit.map(function(elem) { return Math.round((elem - 32) * 5 / 9); }); Formula : Use it when: You want to translate/map all elements in an array to another set of values.

Slide 26

Slide 26 text

Filter array.filter(function(elem, index, array) { ... } Example: remove duplicate elements from an array. var uniqueArray = array.filter(function(elem, index, array) { return array.indexOf(elem) === index; } ); Formula : Use it when: You want to remove unwanted elements based on a condition.

Slide 27

Slide 27 text

Reduce array.reduce(function(prevVal, elem, index, array) { ... }, initialValue); Example: remove duplicate elements from an array. var sum = [0, 1, 2, 3].reduce(function(a, b) { return a + b; }, 0); // sum is 6 Formula : Use it when: You want to find a cumulative or concatenated value based on elements across the array.

Slide 28

Slide 28 text

Some of FP Library • Ramda • lodash/fp • functional.js • lambdajs • immutable.js

Slide 29

Slide 29 text

Awesome FP Resources https://github.com/stoeffel/awesome-fp-js Books :