Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

Functional Programming using underscore.js

othree
April 20, 2013

Functional Programming using underscore.js

othree

April 20, 2013
Tweet

More Decks by othree

Other Decks in Programming

Transcript

  1. me • PhD Candidate • Front End Engineer • On

    the way learning good from functional programming languages
  2. Function of Functional Language • Pure function • First class

    citizen • Higher order function • ...
  3. Pure Function • Side effect free • Same input, same

    output • ex: trigonometric functions
  4. First Class Citizen • Function like variable • ex: function

    expression var f = function () { /*...*/ };
  5. [ { name: 'Gates', gender: 'M', org: 'M$' }, {

    name: 'Peter' gender: 'M', org: 'Hㄒㄈ' } ]
  6. var str = "Mentioned by "; for (var i =

    0, l = tweeps.length; i < l; ++i) { str += tweeps[i].name; if (i < tweeps.length - 1) { str += ", "; } }
  7. [ { name: 'Gates', gender: 'M', org: 'M$' }, {

    name: 'Peter' gender: 'M', org: 'Hㄒㄈ' } ]
  8. var names = []; for (var i = 0, l

    = tweeps.length; i < l; ++i) { names.push(tweeps[i].name); }
  9. [ { getSummary: function () { return { text: 'Summaries',

    html: '<p>Summaries</p>' }; } }, { getSummary: function () { return {text: 'Summaried'}; } }, ... ]
  10. buildSummary: function () { var div = document.createElement("div"), p; for

    (var i = 0, l = this.components.length; i < l; ++i) { p = document.createElement("p"); p.innerHTML = this.components[i].getSummary().text; div.appendChild(p); } return div; }
  11. var ul = cull.dom.el("ul", { className: "bands" }); // var

    li = cull.dom.el("li", "Execration"); var ul = cull.dom.el("ul", { className: "bands" }, li);
  12. ["a", "br", "code", "div", ...].forEach(function (tagName) { cull.dom.el[tagName] = cull.partial(cull.dom.el,

    tagName); }); // ["a", "br", "code", "div", ...].forEach(function (tagName) { root[tagName] = cull.partial(cull.dom.el, tagName); });
  13. buildSummary: function () { return div(this.components. map(function (component) { return

    component.getSummary(); }).map(function (summary) { return summary.text; }).map(function (text) { return p(text); })); }
  14. buildSummary: function () { return div(this.components. map(func("getSummary")). map(function (summary) {

    return summary.text; }).map(function (text) { return p(text); })); }
  15. var callGetSummary = func("getSummary"); var getText = prop("text"); var summarize

    = compose([p, getText, callGetSummary]); // summarize(obj); // => callGetSummary(obj) // => getText(callGetSummary(obj)) // => p(getText(callGetSummary(obj)))
  16. Underscore.js • by Jeremy Ashkenas from DocumentCloud • “Underscore is

    a utility-belt library for JavaScript that provides a lot of the functional programming support”
  17. cull.js • by Christian Johansen and Magnar Sveen • “Cull

    is a toolbelt for writing functional javascript.” • Used in the examples above https://github.com/culljs/culljs
  18. LiveScript & prelude.ls • by George Zahariev • A new

    compile to JavaScript language fork from Coco • Stay in this room until tomorrow, Mindos have a talk about LiveScript
  19. var str = "Mentioned by " + _.reduce( _.map(tweeps, function

    (t) { return t.name; }), function (memo, name, i) { return (i == 0) ? name : memo + ', ' + name; }, '' );
  20. var str = "Mentioned by " + _(tweeps) .chain() .map(function

    (t) { return t.name; }) .reduce(function (memo, name, i) { return (i == 0) ? name : memo + ', ' + name; }, '') .value();
  21. -

  22. @

  23. @ -

  24. @ -

  25. Difference • Better performance • Robust result • Larger file

    size • AMD supports • Auto chain • More power: cloneDeep, partial, result...
  26. _.partial var greet = function(greeting, name) { return greeting +

    ' ' + name; }; var hi = _.partial(greet, 'hi'); hi('moe'); // ! 'hi moe' http://lodash.com/docs#partial
  27. _.result var object = { 'cheese': 'crumpets', 'stuff': function ()

    { return 'nonsense'; } }; _.result(object, 'cheese'); // ! 'crumpets' _.result(object, 'stuff'); // ! 'nonsense' http://lodash.com/docs#result
  28. var summarize = _.compose( p, _.partialRight(_.result, 'name'), _.partialRight(_.result, 'getSummary') );

    // ... buildSummary: function () { return div(_.map(this.components, summarize)); }