functional programming in javascript

Download Functional programming in Javascript

If you can't read please download the document

Upload: knoldus-software-llp

Post on 07-Jan-2017

1.189 views

Category:

Software


1 download

TRANSCRIPT

Functional Programming In JavaScript

Nikhil Kumar | Software Consultant

Knldus Software LLP

Agenda

Functional Programming : The Benefits

Basic version Vs Filter

Find

Map

Reduce

Closures

Now: var, let & const

IIFE

All with comparisons

Functional programming is a paradigm which concentrates on computing results rather than on performing actions. That is, when you call a function, the only significant effect that the function has is usually to compute a value and return it. Of course, behind the scenes the function is using CPU time, allocating and writing memory, but from the programmer's point of view, the primary effect is the return value

F P

Benefits of FP

Less Bugs (less code)

Easier to reason about

Less Time

Re-use more code.

(You can think of the WebView as a chromeless browser window thats typically configured to run fullscreen.)

This enables them to access device capabilities such as the accelerometer, camera, contacts, and more. These are capabilities that are often restricted to access from inside mobile browsers.

Filter

Filter is method on array objects that takes a function as a argument.Filter expect its callback to return true or false.

var porche = cars.filter(function(carName){return carName.brand === 'porche'})

Find

works like as filter, but just return the first value.

var porche = cars.find(function(carName){return carName.brand === 'porche'})

Map

Map is function on the array object.Map expect the callback function to return a transformed object,

Above 3 are used in list transformation, they turn your list into something else.

var carList = []for(var i = 0; i < cars.length; i++){carList.push(cars[i].name)}

Reduce

Reduce is awesome, using reduce you can create functions like, map, find, filter or any other list transformation. Reduce is super List Transformation tool, you can it to meet or your requirements.

var priceSum = cars.reduce(function(sum, cars){console.log("The sum of all cars", + sum, cars);return sum + cars.price},0);

Closures

var myName = "nikhil"

function namesFun(){console.log("Hey the name is " + myName);}

namesFun();

Basically functions in javascript are closures actually, they can access the variables outside of the functions, in languages that does not support closures we cannot do so.Closure is a function inside a function.

Var is function scope let is block scope and don't talk about const here.

Var => let => const

function count(){for(var i = 0; i < 10; i++){console.log(i);}}count();console.log(i);

//let"use strict";

var i = 500; for( i = 0; i< 101; i++){ console.log(i)}

if(true){ /*let*/ i = 34000;}

console.log(" show me ", i);

Using Let

let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used. This is unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope.

function varTest() { var x = 1; if (true) { var x = 2; // same variable! console.log(x); // 2 } console.log(x); // 2}

function letTest() { let x = 1; if (true) { let x = 2; // different variable console.log(x); // 2 } console.log(x); // 1}

Using Const

Constants are block-scoped, much like variables defined using the let statement. The value of a constant cannot change through re-assignment, and it can't be redeclared.

The const declaration creates a read-only reference to a value. It does notmean the value it holds is immutable, just that the variable identifier cannot be reassigned. For instance, in case the content is an object, this means the object itself can still be altered.

"use strict";const x= {y: 10}//this can be done x.y = 11;//BUTx = {z:5;}console.log(x);

IIFE: Immediately Invoked Function Expression:

(function(){for(var i = 0; i < 10; i++){console.log(i);}})();

References

Mozilla Documentation

Piyush Mishra

Presenter [email protected]

Organizer@Knolspeak

http://www.knoldus.comhttp://blog.knoldus.com

Thanks