{ Declarative JavaScripting Functional programming in JavaScript … and the curious case of _.js Om Shankar JavaScripter @ InMobi bit.ly/omshankar
Jan 27, 2015
{
DeclarativeJavaScripting
Functional programming in JavaScript
… and the curious case of _.js
Om ShankarJavaScripter @ InMobi bit.ly/omshankar
Functional programming is the use of functions that transform values into units of abstraction, subsequently used to build software systems.
Functional Programming?
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
{
Core Fundamentals
The building blocks of Functional JavaScript
Closures
Functions constructing FunctionsRecursion
Functions passed as parameters
Mutation policies
Flow and Chaining
Class-less JavaScript
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.
// 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 !!"; }());
- 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
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
- Applicative Declaratives
_.map
reduce / reduceRight
find / detect / where
filter / reject
every / some
...
- Scopes
_.bind(scopeObject, normalFn);
_.bindAll(obj, fn1, [fn2, fn3, ...]);
- Closure• Creating a complement function
• Creating a repeater function
Higher order Functions
Functions taking functions as parameters
function repeat(times, fn) { return _.map(_.range(times), fn);}
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
{
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
{
ClojureScript - github.com/clojure/clojurescript
ROY - bilby.brianmckenna.org
Trees in the Jungle
Declarative JavaScript
Compilers
CoffeScript - coffeescript.org
{Happy JavaScript-ing
… to be continued
@om_invincible
github.com/OmShiv
geekyogi.com
End of Part - 1
bit.ly/omshankar