Functional thinking in coffee script

1,226 views

Published on

Software Craftsmanship Atlanta - Functional Thinking in CoffeeScript - John Napier, ThoughtWorks

  • Be the first to comment

  • Be the first to like this

Functional thinking in coffee script

  1. 1. FIRST, A QUICK POLL
  2. 2. FIRST, A QUICK POLL FUNCTIONAL PROGRAMMING EXPERIENCE? (THE 80’S COUNTS)
  3. 3. FIRST, A QUICK POLL FUNCTIONAL PROGRAMMING EXPERIENCE? (THE 80’S COUNTS) COFFEESCRIPT EXPERIENCE? (NOBODY LIKES A SHOW OFF)
  4. 4. AN OVER-SIMPLIFIED COMPARISON...
  5. 5. AN OVER-SIMPLIFIED COMPARISON... FUNCTIONAL LANGUAGES-  NO SIDE EFFECTS DUE TO LEXICAL SCOPING-  IMMUTABLE OBJECTS-  MONADIC FUNCTIONS (1-ARITY)-  ITERATION THROUGH RECURSION
  6. 6. AN OVER-SIMPLIFIED COMPARISON… FUNCTIONAL JAVASCRIPT / LANGUAGES COFFEESCRIPT-  NO SIDE EFFECTS -  SIDE EFFECTS IF YOU DUE TO LEXICAL WANT THEM, UGH! SCOPING -  VERY FEW-  IMMUTABLE OBJECTS IMMUTABLE OBJECTS-  MONADIC FUNCTIONS -  N-ARITY, VARIADIC (1-ARITY) FUNCTIONS-  ITERATION THROUGH -  ITERATION TYPICALLY RECURSION THROUGH CURSORS
  7. 7. A GROSSLY OVER-SIMPLIFIED COMPARISON…
  8. 8. A GROSSLY OVER-SIMPLIFIED COMPARISON…IMPERATIVELY LANGUAGES (HOW)
  9. 9. A GROSSLY OVER-SIMPLIFIED COMPARISON…IMPERATIVELY LANGUAGES (HOW)-  USING FREE HAND, PICK UP PORK SANDWICH
  10. 10. A GROSSLY OVER-SIMPLIFIED COMPARISON…IMPERATIVELY LANGUAGES (HOW)-  USING FREE HAND, PICK UP PORK SANDWICH-  RAISE HAND TO MOUTH
  11. 11. A GROSSLY OVER-SIMPLIFIED COMPARISON…IMPERATIVELY LANGUAGES (HOW)-  USING FREE HAND, PICK UP PORK SANDWICH-  RAISE HAND TO MOUTH-  OPEN MOUTH
  12. 12. A GROSSLY OVER-SIMPLIFIED COMPARISON…IMPERATIVELY LANGUAGES (HOW)-  USING FREE HAND, PICK UP PORK SANDWICH-  RAISE HAND TO MOUTH-  OPEN MOUTH-  PLACE PORK SANDWICH IN MOUTH
  13. 13. A GROSSLY OVER-SIMPLIFIED COMPARISON…IMPERATIVELY LANGUAGES (HOW)-  USING FREE HAND, PICK UP PORK SANDWICH-  RAISE HAND TO MOUTH-  OPEN MOUTH-  PLACE PORK SANDWICH IN MOUTH-  RELEASE PORK SANDWICH
  14. 14. A GROSSLY OVER-SIMPLIFIED COMPARISON…IMPERATIVELY LANGUAGES (HOW)-  USING FREE HAND, PICK UP PORK SANDWICH-  RAISE HAND TO MOUTH-  OPEN MOUTH-  PLACE PORK SANDWICH IN MOUTH-  RELEASE PORK SANDWICH-  REMOVE HAND FROM MOUTH
  15. 15. A GROSSLY OVER-SIMPLIFIED COMPARISON…IMPERATIVELY LANGUAGES (HOW)-  USING FREE HAND, PICK UP PORK SANDWICH-  RAISE HAND TO MOUTH-  OPEN MOUTH-  PLACE PORK SANDWICH IN MOUTH-  RELEASE PORK SANDWICH-  REMOVE HAND FROM MOUTH-  CLOSE MOUTH
  16. 16. A GROSSLY OVER-SIMPLIFIED COMPARISON…IMPERATIVELY LANGUAGES (HOW)-  USING FREE HAND, PICK UP PORK SANDWICH-  RAISE HAND TO MOUTH-  OPEN MOUTH-  PLACE PORK SANDWICH IN MOUTH-  RELEASE PORK SANDWICH-  REMOVE HAND FROM MOUTH-  CLOSE MOUTH-  OPEN MOUTH
  17. 17. A GROSSLY OVER-SIMPLIFIED COMPARISON…IMPERATIVELY LANGUAGES (HOW)-  USING FREE HAND, PICK UP PORK SANDWICH-  RAISE HAND TO MOUTH-  OPEN MOUTH-  PLACE PORK SANDWICH IN MOUTH-  RELEASE PORK SANDWICH-  REMOVE HAND FROM MOUTH-  CLOSE MOUTH-  OPEN MOUTH-  CLOSE MOUTH
  18. 18. A GROSSLY OVER-SIMPLIFIED COMPARISON…IMPERATIVELY LANGUAGES (HOW)-  USING FREE HAND, PICK UP PORK SANDWICH-  RAISE HAND TO MOUTH-  OPEN MOUTH-  PLACE PORK SANDWICH IN MOUTH-  RELEASE PORK SANDWICH-  REMOVE HAND FROM MOUTH-  CLOSE MOUTH-  OPEN MOUTH-  CLOSE MOUTH-  OPEN MOUTH
  19. 19. A GROSSLY OVER-SIMPLIFIED COMPARISON…IMPERATIVELY LANGUAGES (HOW)-  USING FREE HAND, PICK UP PORK SANDWICH-  RAISE HAND TO MOUTH-  OPEN MOUTH-  PLACE PORK SANDWICH IN MOUTH-  RELEASE PORK SANDWICH-  REMOVE HAND FROM MOUTH-  CLOSE MOUTH-  OPEN MOUTH-  CLOSE MOUTH-  OPEN MOUTH-  CLOSE MOUTH
  20. 20. A GROSSLY OVER-SIMPLIFIED COMPARISON…IMPERATIVELY LANGUAGES (HOW)-  USING FREE HAND, PICK UP PORK SANDWICH-  RAISE HAND TO MOUTH-  OPEN MOUTH-  PLACE PORK SANDWICH IN MOUTH-  RELEASE PORK SANDWICH-  REMOVE HAND FROM MOUTH-  CLOSE MOUTH-  OPEN MOUTH-  CLOSE MOUTH-  OPEN MOUTH-  CLOSE MOUTH-  SWALLOW
  21. 21. A GROSSLY OVER-SIMPLIFIED COMPARISON…IMPERATIVELY LANGUAGES DECLARATIVE LANGUAGES (HOW) (WHAT)-  USING FREE HAND, PICK UP PORK SANDWICH-  RAISE HAND TO MOUTH-  OPEN MOUTH-  PLACE PORK SANDWICH IN MOUTH-  RELEASE PORK SANDWICH-  REMOVE HAND FROM MOUTH-  CLOSE MOUTH-  OPEN MOUTH-  CLOSE MOUTH-  OPEN MOUTH-  CLOSE MOUTH-  SWALLOW
  22. 22. A GROSSLY OVER-SIMPLIFIED COMPARISON…IMPERATIVELY LANGUAGES DECLARATIVE LANGUAGES (HOW) (WHAT)-  USING FREE HAND, PICK UP PORK SANDWICH -  EAT PORK SANDWICH-  RAISE HAND TO MOUTH-  OPEN MOUTH-  PLACE PORK SANDWICH IN MOUTH-  RELEASE PORK SANDWICH-  REMOVE HAND FROM MOUTH-  CLOSE MOUTH-  OPEN MOUTH-  CLOSE MOUTH-  OPEN MOUTH-  CLOSE MOUTH-  SWALLOW
  23. 23. A GROSSLY OVER-SIMPLIFIED COMPARISON…IMPERATIVELY LANGUAGES DECLARATIVE LANGUAGES (HOW) (WHAT)-  USING FREE HAND, PICK UP PORK SANDWICH -  EAT PORK SANDWICH-  RAISE HAND TO MOUTH-  OPEN MOUTH (I SAID “GROSSLY”)-  PLACE PORK SANDWICH IN MOUTH-  RELEASE PORK SANDWICH-  REMOVE HAND FROM MOUTH-  CLOSE MOUTH-  OPEN MOUTH-  CLOSE MOUTH-  OPEN MOUTH-  CLOSE MOUTH-  SWALLOW
  24. 24. …WHICH LEADS US TO A FEW CAVEATS
  25. 25. …WHICH LEADS US TO A FEW CAVEATS COFFEESCRIPT IS NOT PURELY FUNCTIONAL
  26. 26. …WHICH LEADS US TO A FEW CAVEATS COFFEESCRIPT IS NOT PURELY FUNCTIONAL WE DON’T REALLY CARE
  27. 27. …WHICH LEADS US TO A FEW CAVEATS COFFEESCRIPT IS NOT PURELY FUNCTIONAL WE DON’T REALLY CARE IT’S JUST JAVASCRIPT
  28. 28. WITH THAT, LET’S GET STARTED (WHY YOU’RE REALLY HERE)
  29. 29. PROBLEM 1†IF WE LIST ALL THE NATURAL NUMBERS BELOW 10 THAT ARE MULTIPLES OF 3 OR 5, WE GET 3, 5, 6 AND 9. THE SUM OF THESE MULTIPLES IS 23. FIND THE SUM OF ALL THE MULTIPLES OF 3 OR 5 BELOW 1000. † HTTP://PROJECTEULER.NET/PROBLEM=1
  30. 30. PROBLEM 1†IF WE LIST ALL THE NATURAL NUMBERS BELOW 10 THAT ARE MULTIPLES OF 3 OR 5, WE GET 3, 5, 6 AND 9. THE SUM OF THESE MULTIPLES IS 23. FIND THE SUM OF ALL THE MULTIPLES OF 3 OR 5 BELOW 1000. (VOLUNTEERS?) † HTTP://PROJECTEULER.NET/PROBLEM=1
  31. 31. PROBLEM 1†IF WE LIST ALL THE NATURAL NUMBERS BELOW 10 THAT ARE MULTIPLES OF 3 OR 5, WE GET 3, 5, 6 AND 9. THE SUM OF THESE MULTIPLES IS 23. FIND THE SUM OF ALL THE MULTIPLES OF 3 OR 5 BELOW 1000. FUNCTIONS WE’LL USE: FILTER, REDUCE † HTTP://PROJECTEULER.NET/PROBLEM=1
  32. 32. [1, 2, 3, 4].FILTER(FUNCTION(X){ RETURN X % 2 == 0;});//-> [2, 4][‘THE’, ‘RAIN’, ‘IN’, ‘SPAIN’].FILTER( FUNCTION(WORD){ RETURN WORD.LENGTH > 4; });//-> [‘SPAIN’]
  33. 33. [2, 4, 6].REDUCE(FUNCTION(SUM, X){ RETURN SUM + X;});//-> 12[1, 2, 3].REDUCE( FUNCTION(STR, X){ RETURN STR + ‘…‘ + X; }, ‘TESTING…TESTING’);//-> ‘TESTING…TESTING…1…2…3’
  34. 34. JAVASCRIPT544 CHARACTERS
  35. 35. COFFEESCRIPT248 CHARACTERS
  36. 36. PROBLEM 2† THE SUM OF THE SQUARES OF THE FIRST TEN NATURAL NUMBERS IS, 1^2 + 2^2 + ... + 10^2 = 385 THE SQUARE OF THE SUM OF THE FIRST TEN NATURAL NUMBERS IS, (1 + 2 + ... + 10)^2 = 55^2 = 3025 HENCE THE DIFFERENCE BETWEEN THE SUM OF THE SQUARES OF THE FIRST TEN NATURAL NUMBERS AND THE SQUARE OF THE SUM IS 3025 - 385 = 2640. FIND THE DIFFERENCE BETWEEN THE SUM OF THE SQUARES OF THE FIRST ONE HUNDRED NATURAL NUMBERS AND THE SQUARE OF THE SUM. † HTTP://PROJECTEULER.NET/PROBLEM=6
  37. 37. PROBLEM 2† THE SUM OF THE SQUARES OF THE FIRST TEN NATURAL NUMBERS IS, 1^2 + 2^2 + ... + 10^2 = 385 THE SQUARE OF THE SUM OF THE FIRST TEN NATURAL NUMBERS IS, (1 + 2 + ... + 10)^2 = 55^2 = 3025 HENCE THE DIFFERENCE BETWEEN THE SUM OF THE SQUARES OF THE FIRST TEN NATURAL NUMBERS AND THE SQUARE OF THE SUM IS 3025 - 385 = 2640. FIND THE DIFFERENCE BETWEEN THE SUM OF THE SQUARES OF THE FIRST ONE HUNDRED NATURAL NUMBERS AND THE SQUARE OF THE SUM. (VOLUNTEERS?) † HTTP://PROJECTEULER.NET/PROBLEM=6
  38. 38. PROBLEM 2† THE SUM OF THE SQUARES OF THE FIRST TEN NATURAL NUMBERS IS, 1^2 + 2^2 + ... + 10^2 = 385 THE SQUARE OF THE SUM OF THE FIRST TEN NATURAL NUMBERS IS, (1 + 2 + ... + 10)^2 = 55^2 = 3025 HENCE THE DIFFERENCE BETWEEN THE SUM OF THE SQUARES OF THE FIRST TEN NATURAL NUMBERS AND THE SQUARE OF THE SUM IS 3025 - 385 = 2640. FIND THE DIFFERENCE BETWEEN THE SUM OF THE SQUARES OF THE FIRST ONE HUNDRED NATURAL NUMBERS AND THE SQUARE OF THE SUM. FUNCTIONS WE’LL USE: MAP, REDUCE † HTTP://PROJECTEULER.NET/PROBLEM=6
  39. 39. [‘ALAN’, ‘BOB’, ‘FRANK’].MAP(FUNCTION(NAME){ RETURN NAME.LENGTH;});//-> [4, 3, 5][1, 2, 3].MAP(FUNCTION(X){ RETURN X * X;});//-> [1, 4, 9]
  40. 40. JAVASCRIPT547 CHARACTERS
  41. 41. COFFEESCRIPT263 CHARACTERS
  42. 42. PROBLEM 3†A PALINDROMIC NUMBER READS THE SAME BOTH WAYS. THE LARGEST PALINDROME MADE FROM THE PRODUCT OF TWO 2-DIGIT NUMBERS IS 9009 = 91 X 99. FIND THE LARGEST PALINDROME MADE FROM THE PRODUCT OF TWO 3-DIGIT NUMBERS. † HTTP://PROJECTEULER.NET/PROBLEM=4
  43. 43. PROBLEM 3†A PALINDROMIC NUMBER READS THE SAME BOTH WAYS. THE LARGEST PALINDROME MADE FROM THE PRODUCT OF TWO 2-DIGIT NUMBERS IS 9009 = 91 X 99. FIND THE LARGEST PALINDROME MADE FROM THE PRODUCT OF TWO 3-DIGIT NUMBERS. (VOLUNTEERS?) † HTTP://PROJECTEULER.NET/PROBLEM=4
  44. 44. PROBLEM 3†A PALINDROMIC NUMBER READS THE SAME BOTH WAYS. THE LARGEST PALINDROME MADE FROM THE PRODUCT OF TWO 2-DIGIT NUMBERS IS 9009 = 91 X 99. FIND THE LARGEST PALINDROME MADE FROM THE PRODUCT OF TWO 3-DIGIT NUMBERS. FUNCTIONS WE’LL USE: MAP, FILTER, REDUCE † HTTP://PROJECTEULER.NET/PROBLEM=4
  45. 45. JAVASCRIPT1,068 CHARACTERS
  46. 46. COFFEESCRIPT462 CHARACTERS
  47. 47. OKAY JOHN, VERY GOOD. (ANYTHING USEFUL TODAY?)
  48. 48. REALISTIC PROBLEMASSUME WE HAVE A BUNCH OF DOCUMENTS, REPRESENTED AS STRINGS (YES, AN ANEMIC DOMAIN. I SAID IT’D BE REALISTIC).DESIGN A DOCUMENT DATABASE THAT TAKES A COLLECTION OF DOCUMENTS AND RETURNS THE SIZE OF THE LARGEST ONE
  49. 49. REALISTIC PROBLEMASSUME WE HAVE A BUNCH OF DOCUMENTS, REPRESENTED AS STRINGS (YES, AN ANEMIC DOMAIN. I SAID IT’D BE REALISTIC).DESIGN A DOCUMENT DATABASE THAT TAKES A COLLECTION OF DOCUMENTS AND RETURNS THE SIZE OF THE LARGEST ONE (VOLUNTEERS?)
  50. 50. JAVASCRIPT642 CHARACTERS
  51. 51. JAVASCRIPT601 CHARACTERS
  52. 52. COFFEESCRIPT546 CHARACTERS
  53. 53. COFFEESCRIPT278 CHARACTERS
  54. 54. QUESTIONS BEFORE WE PAIR? (OR DURING, THAT’S COOL TOO)
  55. 55. PAIRING CHALLENGEA COLLATZ SEQUENCE TAKES A NUMBER. IF THAT NUMBER IS EVEN, IT’S DIVIDED BY BY 2. IF IT’S ODD, IT’S MULTIPLIED BY 3 AND ADDED TO 1. THE RESULTING NUMBER IS THEN REAPPLIED TO THE COLLATZ FORMULA, UNTIL WE GET BACK THE NUMBER 1, AT WHICH POINT THE COLLATZ SEQUENCE TERMINATES. FOR ALL STARTING NUMBERS BETWEEN 1 AND 100, HOW MANY COLLATZ SEQUENCES HAVE A LENGTH GREATER THAN 15?

×