Top Banner
{ Declarative JavaScripting Functional programming in JavaScript … and the curious case of _.js Om Shankar JavaScripter @ InMobi bit.ly/omshankar
15

Declarative JavaScript concepts and implemetation

Jan 27, 2015

Download

Technology

Om Shankar

Declarative style JavaScripting and the curious case of underscore.js.
- Presented at BangaloreJS Eleventh meetup: http://bangalorejs.org/eleventh.html
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Declarative JavaScript concepts and implemetation

{

DeclarativeJavaScripting

Functional programming in JavaScript

… and the curious case of _.js

Om ShankarJavaScripter @ InMobi bit.ly/omshankar

Page 2: Declarative JavaScript concepts and implemetation

Functional programming is the use of functions that transform values into units of abstraction, subsequently used to build software systems.

Functional Programming?

Page 3: Declarative JavaScript concepts and implemetation

Imperative vs Declarative

Functional Programming?

Do Something !

– by doing this, and then that, – and don’t forget to do this at the end of all that.

Get Something Done !

– I don’t care how you do it,– also, when I want it done again, it should be as easy as it was the first time.

Declarative programming: stackoverflow.com/a/8357604/1249219

Page 4: Declarative JavaScript concepts and implemetation

{

Core Fundamentals

The building blocks of Functional JavaScript

Closures

Functions constructing FunctionsRecursion

Functions passed as parameters

Mutation policies

Flow and Chaining

Class-less JavaScript

Page 5: Declarative JavaScript concepts and implemetation

Function Rules

• Should travel in First Class

• Should be Pure, hence, should be re-

usable

• Should be Applicative

- function A supplied to B should perform actions in B’s

context.

Page 6: Declarative JavaScript concepts and implemetation

// Both can be storedvar string = "BangaloreJS";var stringFn = function() { return "BangaloreJS"; }

- First Class CitizensStrings are first class in JS, - so are functions with all possible operations

// can be used on the fly, as an expression:

// string"BangaloreJS";"BangaloreJS".slice(3);

// function"BangaloreJS" + (function(){ return " Rocks !!"; }());

Page 7: Declarative JavaScript concepts and implemetation

- Purity of FunctionsA pure function

• takes argument(s)• returns value(s)• returns same value(s) for same

argument(s)• does nothing else !

Arguments Return value

Page 8: Declarative JavaScript concepts and implemetation

An impure function

• takes argument(s)• returns value(s)• reads/writes external states, for eg.,

modifies global DOM, objects, arrays.

- Purity of Functions

Arguments Return value

Change State

Read State

tightly coupled with read state

Page 9: Declarative JavaScript concepts and implemetation

- Applicative Declaratives

_.map

reduce / reduceRight

find / detect / where

filter / reject

every / some

...

Page 10: Declarative JavaScript concepts and implemetation

- Scopes

_.bind(scopeObject, normalFn);

_.bindAll(obj, fn1, [fn2, fn3, ...]);

- Closure• Creating a complement function

• Creating a repeater function

Page 11: Declarative JavaScript concepts and implemetation

Higher order Functions

Functions taking functions as parameters

function repeat(times, fn) { return _.map(_.range(times), fn);}

Page 12: Declarative JavaScript concepts and implemetation

Higher order Functions

Functions returning functions as value

var numData = [1,2,null,4,5];

_.reduce(numData, function(tot, curr) { return tot * curr;});

// Problem?// Solution: function returning modified safe function// and not imperatively modified data

// though what we actually do is modify data on the fly

Page 13: Declarative JavaScript concepts and implemetation

{

Trees in the Jungle

Declarative JavaScript

RxJS- Reactive extensions for JS by Microsoft

Bilby.js - bilby.brianmckenna.org

Allong.es - github.com/fogus/lemonad

Libraries

Page 14: Declarative JavaScript concepts and implemetation

{

ClojureScript - github.com/clojure/clojurescript

ROY - bilby.brianmckenna.org

Trees in the Jungle

Declarative JavaScript

Compilers

CoffeScript - coffeescript.org

Page 15: Declarative JavaScript concepts and implemetation

{Happy JavaScript-ing

… to be continued

@om_invincible

github.com/OmShiv

geekyogi.com

End of Part - 1

bit.ly/omshankar