{

Functional programming in JavaScript
… and the curious case of _.js

Om Shankar
JavaScripter @ InMobi

bit.ly/omshankar
Functional programming is the use of functions that
transform values into units of abstraction, subsequently
used to build...
Do Something !
– by doing this, and then that,
– and don’t forget to do this at the end of all that.

Get Something Done !...
Closures

Flow and Chaining

Recursion

{

Functions constructing Functions

The building blocks of Functional JavaScript
...
• Should travel in First Class
• Should be Pure, hence, should be re-usable
• Should be Applicative
- function A supplied ...
Strings are first class in JS,
- so are functions with all possible operations
// Both can be stored
var string = "Bangalo...
A pure function
•
•
•
•

takes argument(s)
returns value(s)
returns same value(s) for same argument(s)
does nothing else !...
An impure function
• takes argument(s)
• returns value(s)
• reads/writes external states, for
eg., modifies global DOM, ob...
_. map
reduce / reduceRight
find / detect / where
filter / reject
every / some
...
_.bind(scopeObject, normalFn);
_.bindAll(obj, fn1, [fn2, fn3, ...]);

• Creating a complement function
• Creating a repeat...
Functions taking functions as parameters

function repeat(times, fn) {
return _.map(_.range(times), fn);
}
Functions returning functions as value
var numData = [1,2,null,4,5];
_.reduce(numData, function(tot, curr) {
return tot * ...
RxJS
- Reactive extensions for JS by Microsoft

{

Declarative JavaScript Libraries

Bilby.js - bilby.brianmckenna.org
All...
CoffeScript - coffeescript.org

{

Declarative JavaScript Compilers

ClojureScript - github.com/clojure/clojurescript

ROY...
@om_invincible

{

geekyogi.com

End of Part - 1
Happy JavaScript-ing

github.com/OmShiv

bit.ly/omshankar
Upcoming SlideShare
Loading in …5
×

Declarative JavaScript concepts and implemetation

883 views
777 views

Published on

Declarative style JavaScripting and the curious case of underscore.js.
- Presented at BangaloreJS Eleventh meetup: http://bangalorejs.org/eleventh.html

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
883
On SlideShare
0
From Embeds
0
Number of Embeds
16
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Declarative JavaScript concepts and implemetation

  1. 1. { Functional programming in JavaScript … and the curious case of _.js Om Shankar JavaScripter @ InMobi bit.ly/omshankar
  2. 2. Functional programming is the use of functions that transform values into units of abstraction, subsequently used to build software systems.
  3. 3. 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
  4. 4. Closures Flow and Chaining Recursion { Functions constructing Functions The building blocks of Functional JavaScript Mutation policies Functions passed as parameters Class-less JavaScript
  5. 5. • 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.
  6. 6. Strings are first class in JS, - so are functions with all possible operations // Both can be stored var string = "BangaloreJS"; var stringFn = function() { return "BangaloreJS"; } // can be used on the fly, as an expression: // string "BangaloreJS"; "BangaloreJS".slice(3); // function "BangaloreJS" + (function(){ return " Rocks !!"; }());
  7. 7. A pure function • • • • takes argument(s) returns value(s) returns same value(s) for same argument(s) does nothing else ! Arguments Return value
  8. 8. An impure function • takes argument(s) • returns value(s) • reads/writes external states, for eg., modifies global DOM, objects, arrays. Read State Arguments Return value tightly coupled with read state Change State
  9. 9. _. map reduce / reduceRight find / detect / where filter / reject every / some ...
  10. 10. _.bind(scopeObject, normalFn); _.bindAll(obj, fn1, [fn2, fn3, ...]); • Creating a complement function • Creating a repeater function
  11. 11. Functions taking functions as parameters function repeat(times, fn) { return _.map(_.range(times), fn); }
  12. 12. 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
  13. 13. RxJS - Reactive extensions for JS by Microsoft { Declarative JavaScript Libraries Bilby.js - bilby.brianmckenna.org Allong.es - github.com/fogus/lemonad
  14. 14. CoffeScript - coffeescript.org { Declarative JavaScript Compilers ClojureScript - github.com/clojure/clojurescript ROY - bilby.brianmckenna.org
  15. 15. @om_invincible { geekyogi.com End of Part - 1 Happy JavaScript-ing github.com/OmShiv bit.ly/omshankar

×