Functional programming

In Javascript

In computer science, 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

Imperative Programming

Object Oriented Style

Functional Style

Functions

as first class objects

Demo

"Talk is cheap , show me the code"

Sum of Array

var arr = {1,2,3}
arr[4] = 4;


//Imperative Style
var sum = 0;
for(var i =0;i<arr.length;i++){
 sum = sum + arr[i]
}

console.log(sum); //NaN


//Functional Style
//Abstract out the addition

console.log(arr.reduce(sum) ); //10

_.reduce

The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) has to reduce it to a single value.

 _.reduce([1, 2, 3], function(acc, num){ return acc + num; }, 0)

Demo

 

Higher Order Functions

Function Composition

Tail Recursion

 

Codependent Functions
 

Don’t modify objects you don’t own

Nicholas Zakas

var Line = server.getLine();

//pass that around

Line.inventory.revenue = 100;
 var line = Line.init(server.getLine());
line.updateRevenue(1000);
  1. Single Point of Mutation
  2. Allows Domain specific Constaints

Flow Based Programming

Concept Of Thunks , Chaining and PipeLining

Line.calculateDiscount(10).calculateInventoryRevenue().calculateExpectedPrice().value();

Function PipeLining

(The thrush Combinator)

In object-oriented programming languages, a mixin is a class that contains a combination of methods from other classes. How such a combination is done depends on the language, but it is not by inheritance.

Mixins

Architectures

MVC

Flux

ReFlux

Redux

Some Libraries

Q n A

- Abhik

Made with Slides.com