06 Map Reduce

280 views

Published on

Examples and concept from the excellent Clojure talk, "A monad is not a burrito."

http://www.youtube.com/watch?v=46Z7Hq4fhN0

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
280
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

06 Map Reduce

  1. 1. Let’s talk about lambda
  2. 2. Why care?
  3. 3. NULL is troublesome The dog breeder example
  4. 4. var colleen, richard; ! colleen = { pet: { mother: { owner: "Sarah" } } }; ! richard = { pet: null };
  5. 5. Given a person, return its pet’s mother’s owner.
  6. 6. function findBreeder(person) { return person.pet.mother.owner; }
  7. 7. function findBreeder(person) { return person.pet.mother.owner; } ! >>> findBreeder(colleen); "Sarah" ! >>> findBreeder(richard); Exception: "Can not read property 'mother' of undefined"
  8. 8. function findBreeder(person) { if (!person) { return null; } ! if (!person.pet) { return null; } if (!person.pet.mother) { return null; } ! return person.pet.mother.owner; }
  9. 9. >>> findBreeder(colleen); "Sarah" ! >>> findBreeder(richard); null
  10. 10. LISTS are troublesome The investment broker example
  11. 11. Broker Clients Investment Types Markets Balance
  12. 12. var colin = { clients: [ { name: "Fred", investments: [ { name: "Shares", markets: [ { name: "Japan", value: 7000 }, { name: "America", value: 8888 } ] }, { name: "Gold", markets: [ { name: "China", value: 3333 }, ] } ] } ] };
  13. 13. Find the sum of all of an investors’s client’s investments.
  14. 14. sumInvestments = (clients) -> values = [] foreach client in clients: foreach investment in client.investments foreach market in investment.markets values.push(market.balance) return sum(values)
  15. 15. function sumInvestments (clients) { var i, j, k; var investments, markets; var value = 0; ! for (i = 0; i < clients.length; i++) { investments = clients[i].investments; ! for (j = 0; j < investments.length; j++) { markets = investments[j].markets; ! for (k = 0; k < markets.length; k++) { value += markets[k].value } } } ! return value; };
  16. 16. (multi-layer list iteration) vs. (null checking) ! What’s the common thread?
  17. 17. no code duplication ! lots of concept duplication
  18. 18. function call_unless_null(val, fn) { if (val !== null) { return fn(val); } ! return null; } ! function findBreeder(person) { return call_unless_null(person, function(person) { return call_unless_null(person.pet, function(pet) { return call_unless_null(pet.mother, function(mother) { return mother.owner; }); }); }); }
  19. 19. >>> findBreeder(colleen); "Sarah" ! >>> findBreeder(richard); null
  20. 20. Still too much duplication.
  21. 21. function reduce(iter, memo, fn) { for (var i = 0; i < iter.length; i++) { memo = fn(memo, iter[i]); } ! return memo; } ! function get_unless_null(obj, key) { if (obj !== null && obj !== undefined) { return obj[key]; } return null; } ! function findBreeder(person) { return reduce(['pet', 'mother', ‘owner’], person, get_unless_null); }
  22. 22. >>> findBreeder(colleen); "Sarah" ! >>> findBreeder(richard); null
  23. 23. function sumInvestments(clients) { var values; ! values = flat_map(clients, function(client) { return flat_map(client.investments, function(investment) { return flat_map(investment.markets, function(market) { return market.value; }); }); }); ! return sum(values); };
  24. 24. function map(iter, fn) { var out = [], val; ! for (var i = 0; i < iter.length; i++) { out[i] = fn(iter[i]); } ! return out; } ! function flat_map(iter, fn) { iter = map(iter, fn); return reduce(iter, [], function(memo, item) { return memo.concat(item); }); } ! function sum(list) { return reduce(list, 0, function(memo, num) { return memo + num; }); }
  25. 25. function sumInvestments(clients) { var v, fn; ! fn = function(arr, key) { return flat_map(arr, function(item) { return get_unless_null(item, key); }); }; ! v = reduce(clients, ['investments', 'markets', 'value'], fn); return sum(v); };
  26. 26. What have we accomplished?
  27. 27. function sumInvestments (clients) { var i, j, k; var investments, markets; var value = 0; ! ! ugly for (i = 0; i < clients.length; i++) { investments = clients[i].investments; for (j = 0; j < investments.length; j++) { markets = investments[j].markets; ! for (k = 0; k < markets.length; k++) { value += markets[k].value } } ! } return value }; ! function sumInvestments(clients) { var v; ! ! }; awesome v = reduce(clients, ['investments', 'markets', 'value'], function(arr, key) { return flat_map(arr, function(item) { return item[key]; }); }); return sum(v);
  28. 28. ugly function findBreeder(person) { if (!person) { return null; } ! if (!person.pet) { return null; } if (!person.pet.mother) { return null; } ! return person.pet.mother.owner; } ! ! ! awesome function findBreeder(person) { return reduce(get_unless_null, person, ['pet', 'mother', 'owner']); }
  29. 29. Concept duplication is just masked code duplication
  30. 30. Repeating patterns are hint
  31. 31. Higher-order functions make all this possible

×