Elements of Functional Programming in JS

Elements of Functional Programming in JS

Lightning talk from Meet.js Wrocław.

Adam Stankiewicz

May 27, 2014

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

21 slides
  1. What is functional programming? No generally accepted definition, but: •

    Style of building computer programs • Avoids state and mutable data • Functions as arguments and results
  2. Benefits of functional programming • More concise, readable code •

    Confident refactor, better reasoning • Better concurrency? • Performance (function inlining)
  3. JS lacks many functional elements • Tail call optimization •

    Pattern matching • Forced immutability (type system) • No standard methods for FP (IE <=8) ◦ Array#forEach, Array#map, Array#filter, etc...
  4. Short map syntax var characters = [ { 'id': 1,

    'name': 'barney', 'age': 26 }, { 'id': 2, 'name': 'fred', 'age': 30 } ]; _.map(characters, function(c) { return c['name']; }) // ['barney', 'fred'] _.map(characters, 'name') // ['barney', 'fred']
  5. Short filter syntax var characters = [ { 'id': 1,

    'name': 'barney', 'age': 26 }, { 'id': 2, 'name': 'fred', 'age': 30 } ]; _.filter(characters, function(c) { return c.age == 30; }) // [{ 'id': 2, 'name': 'fred', 'age': 30 }] _.filter(characters, { age: 30 }) // [{ 'id': 2, 'name': 'fred', 'age': 30 }]
  6. General pattern // function(e) { return e['foo']; } // 'foo'

    _.sortBy(characters, 'name') // function(e) { return e['foo'] == bar; } // { 'foo': bar } _.every(characters, { 'name': 'barney' })
  7. IndexBy var users = [ { 'id': 100, 'name': 'Adam'

    }, { 'id': 200, 'name': 'Ala' } ]; var indexedUsers = _.indexBy(users, 'id') indexedUsers[100] // { 'id': 100, 'name': 'Adam' } indexedUsers[200] // { 'id': 100, 'name': 'Ala' }
  8. Find var users = [ { 'id': 100, 'name': 'Adam'

    }, { 'id': 200, 'name': 'Ala' } ]; _.find(users, { 'id': 100 }); // { 'id': 100, 'name': 'Adam' }
  9. max, min var characters = [ { 'name': 'barney', 'age':

    26 }, { 'name': 'fred', 'age': 30 } ]; _.max(characters, 'age') // { 'name': 'fred', 'age': 30 } _.min(characters, 'age') // { 'name': 'barney', 'age': 26 }
  10. Composing functions var characters = [ { 'name': 'barney', 'age':

    26 }, { 'name': 'fred', 'age': 30 }, // ... ]; var ages = _.map(characters, 'age'); // [26, 30] _.reduce(ages, function(a, b) { return a + b }); // [56]
  11. Composing functions var characters = [ { 'name': 'barney', 'age':

    26 }, { 'name': 'fred', 'age': 30 }, // ... ]; _(characters) .map('age') .reduce(function(a, b) { return a + b; });
  12. Composing functions var characters = [ { 'name': 'barney', 'age':

    26 }, { 'name': 'fred', 'age': 30 }, // ... ]; _(characters) .map('age') .reduce(function(a, b) { return a + b; });
  13. Other features • Set operations (difference, union, intersection) • every,

    some, reject, countBy, contains • clone, extend, defaults, merge, mapValues • delay, defer, memoize, once, throttle • now, random, uniqueId, template
  14. Promises A+ • Pattern for running async code • Allows

    for return in callbacks • Safely handles exceptions in callbacks • Allows for try/catch -like behavior • Chaining of callbacks, parallel execution promise.then(onSuccess, onFailure)

More Decks by Adam Stankiewicz

Other Decks in Programming