Thanks to visit codestin.com
Credit goes to www.slideshare.net

Functional Programming
With JavaScript
Deepankar Chopra
Senior Software Engineer
Ticketmaster
» What is Functional Programming?
» Mutations
» Side-Effects
» Pure Functions
» Higher Order Functions
» Composition
» Advantages & Disadvantages of FP
» How does FP differ from Object-Oriented programming
2Overview
What is Functional Programming?
Functional programming (often abbreviated FP) is the process of
building software by
» composing pure functions,
» avoiding
» shared state,
» mutable data
» side-effects.
» Managing state flows through composition
3
Mutations
Mutable is a type of variable that can be changed after it is created.
In JavaScript, only objects and arrays are mutable, not primitive
values.
(You can make a variable name point to a new value, but the
previous value is still held in memory.)
4
Mutations 5
let a = { foo: 'bar' };
let b = a;
a.foo = 'test’;
console.log(a,b);
//{foo: "test"} {foo: "test"}
// avoiding mutation
let a = { foo: 'bar' };
let b = Object.assign({}, a);
a.foo = 'test’;
console.log(a,b);
//{foo: "test"} {foo: "bar"}
Side Effects
If a program / function modifies the state of something else
(outside its own scope), then it is producing a side effect.
This could effectively include something like "display a character on
the screen", or it could be changing the value stored in some
arbitrary RAM location, or anything similar.
6
Side Effects include
» Modifying any external variable or object property (e.g., a global
variable, or a variable in the parent function scope chain)
» Logging to the console
» Writing to the screen
» Writing to a file
» Writing to the network
» Triggering any external process
» Calling any other functions with side-effects
7
Pure Functions
» Must return a value
» Can’t produce any side-effects
» Must return the same output for a given input
8
Pure Functions Example 9
let a = 10;
function impure1(x) {
return x + a;
}
function impure2(x) {
a = 30;
return x + a;
}
console.log(impure1(10));//20
a+=10;
console.log(impure1(10));//30
console.log(impure2(10));//40
function pure(x, y) {
return x + y;
}
console.log(pure(10,10));
Composition
Function Composition is a mathematical concept, by which the
result of one function becomes the input of the next, and so on.
Composing functions together is like putting together a series of
pipes for our data to flow through.
10
Composition Example
function addOne(x) {
return x + 1;
}
function timesTwo(x) {
return x * 2;
}
console.log(addOne(timesTwo(3))); //7
11
Higher Order Functions
A higher-order function is a function that does at least one of the
following:
» takes one or more functions as arguments
» returns a function as its result.
12
Higher Order Functions Example
function greaterThan(n) {
return function(m) { return m > n; };
}
let greaterThan10 = greaterThan(10);
let greaterThan11 = greaterThan(11);
console.log(greaterThan10(11));//true
console.log(greaterThan11(11));//false
13
Advantages of FP
» Pure functions are easier to reason about
» Testing is easier, and pure functions lend themselves well to
techniques like property-based testing
» Debugging is easier
» Programs are more bulletproof
» Programs are written at a higher level, and are therefore easier
to comprehend
» Parallel/concurrent programming is easier
14
Disadvantages of FP
» Not suitable in every situation
» Needs to be clubbed with Object oriented programming in some
cases.
» Makes heavy use of recursion
15
How does FP differ from OOP 16
Functional Programming
» Primary Manipulation Unit is
“Function”
» Stateless Programming Model
» Functions with No-Side Effects
» Order of execution is less
importance
Object Oriented Programming
» Primary Manipulation Unit is
Objects(Instances of Classes)
» Stateful Programming Model
» Methods with Side Effects
» Order of execution is more
important.
Any questions?
You can mail me at
deepankar.chopra@gmail.com
17THANKS!

Functional Programming with Javascript

  • 1.
    Functional Programming With JavaScript DeepankarChopra Senior Software Engineer Ticketmaster
  • 2.
    » What isFunctional Programming? » Mutations » Side-Effects » Pure Functions » Higher Order Functions » Composition » Advantages & Disadvantages of FP » How does FP differ from Object-Oriented programming 2Overview
  • 3.
    What is FunctionalProgramming? Functional programming (often abbreviated FP) is the process of building software by » composing pure functions, » avoiding » shared state, » mutable data » side-effects. » Managing state flows through composition 3
  • 4.
    Mutations Mutable is atype of variable that can be changed after it is created. In JavaScript, only objects and arrays are mutable, not primitive values. (You can make a variable name point to a new value, but the previous value is still held in memory.) 4
  • 5.
    Mutations 5 let a= { foo: 'bar' }; let b = a; a.foo = 'test’; console.log(a,b); //{foo: "test"} {foo: "test"} // avoiding mutation let a = { foo: 'bar' }; let b = Object.assign({}, a); a.foo = 'test’; console.log(a,b); //{foo: "test"} {foo: "bar"}
  • 6.
    Side Effects If aprogram / function modifies the state of something else (outside its own scope), then it is producing a side effect. This could effectively include something like "display a character on the screen", or it could be changing the value stored in some arbitrary RAM location, or anything similar. 6
  • 7.
    Side Effects include »Modifying any external variable or object property (e.g., a global variable, or a variable in the parent function scope chain) » Logging to the console » Writing to the screen » Writing to a file » Writing to the network » Triggering any external process » Calling any other functions with side-effects 7
  • 8.
    Pure Functions » Mustreturn a value » Can’t produce any side-effects » Must return the same output for a given input 8
  • 9.
    Pure Functions Example9 let a = 10; function impure1(x) { return x + a; } function impure2(x) { a = 30; return x + a; } console.log(impure1(10));//20 a+=10; console.log(impure1(10));//30 console.log(impure2(10));//40 function pure(x, y) { return x + y; } console.log(pure(10,10));
  • 10.
    Composition Function Composition isa mathematical concept, by which the result of one function becomes the input of the next, and so on. Composing functions together is like putting together a series of pipes for our data to flow through. 10
  • 11.
    Composition Example function addOne(x){ return x + 1; } function timesTwo(x) { return x * 2; } console.log(addOne(timesTwo(3))); //7 11
  • 12.
    Higher Order Functions Ahigher-order function is a function that does at least one of the following: » takes one or more functions as arguments » returns a function as its result. 12
  • 13.
    Higher Order FunctionsExample function greaterThan(n) { return function(m) { return m > n; }; } let greaterThan10 = greaterThan(10); let greaterThan11 = greaterThan(11); console.log(greaterThan10(11));//true console.log(greaterThan11(11));//false 13
  • 14.
    Advantages of FP »Pure functions are easier to reason about » Testing is easier, and pure functions lend themselves well to techniques like property-based testing » Debugging is easier » Programs are more bulletproof » Programs are written at a higher level, and are therefore easier to comprehend » Parallel/concurrent programming is easier 14
  • 15.
    Disadvantages of FP »Not suitable in every situation » Needs to be clubbed with Object oriented programming in some cases. » Makes heavy use of recursion 15
  • 16.
    How does FPdiffer from OOP 16 Functional Programming » Primary Manipulation Unit is “Function” » Stateless Programming Model » Functions with No-Side Effects » Order of execution is less importance Object Oriented Programming » Primary Manipulation Unit is Objects(Instances of Classes) » Stateful Programming Model » Methods with Side Effects » Order of execution is more important.
  • 17.