Your SlideShare is downloading. ×
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
06 Map Reduce
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

06 Map Reduce

84

Published on

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

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

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Let’s talk about lambda
  • 2. Why care?
  • 3. NULL is troublesome The dog breeder example
  • 4. var colleen, richard; ! colleen = { pet: { mother: { owner: "Sarah" } } }; ! richard = { pet: null };
  • 5. Given a person, return its pet’s mother’s owner.
  • 6. function findBreeder(person) { return person.pet.mother.owner; }
  • 7. function findBreeder(person) { return person.pet.mother.owner; } ! >>> findBreeder(colleen); "Sarah" ! >>> findBreeder(richard); Exception: "Can not read property 'mother' of undefined"
  • 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. >>> findBreeder(colleen); "Sarah" ! >>> findBreeder(richard); null
  • 10. LISTS are troublesome The investment broker example
  • 11. Broker Clients Investment Types Markets Balance
  • 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. Find the sum of all of an investors’s client’s investments.
  • 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. 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. (multi-layer list iteration) vs. (null checking) ! What’s the common thread?
  • 17. no code duplication ! lots of concept duplication
  • 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. >>> findBreeder(colleen); "Sarah" ! >>> findBreeder(richard); null
  • 20. Still too much duplication.
  • 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. >>> findBreeder(colleen); "Sarah" ! >>> findBreeder(richard); null
  • 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. 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. 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. What have we accomplished?
  • 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. 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. Concept duplication is just masked code duplication
  • 30. Repeating patterns are hint
  • 31. Higher-order functions make all this possible

×