The Power of Small Abstractions

The Power of Small Abstractions

Talk given at:
XConf (Hamburg, July 7th 2015)
XConf (Manchester, July 9th 2015)
Eurucamp (Berlin, August 2nd 2015)
JSConf EU (Berlin, September 25-27th 2015)

3db71281264f6125e764d9f3788301e0?s=128

Josep M. Bach (Txus)

July 07, 2015
Tweet

Transcript

  1. The power of small abstractions. () = ?

  2. Txus. /t∫us/

  3. None
  4. None
  5. Abstraction. By Smok Bazyli (Own work) [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0)],

    via Wikimedia Commons Image taken by Robert Southworth, 2004-9-4.
  6. Abstraction.

  7. “I suspect, nonetheless, that he wasn’t much capable of thinking.

    To think is to forget differences, to generalise, to abstract.” Jorge Luis Borges
  8. None
  9. None
  10. 1 + 1 2 =

  11. “hello” + “world” “helloworld” =

  12. [1,2] + [3,4] [1,2,3,4] =

  13. + =

  14. 1 + (1 + 1) (1 + 1) + 1

    =
  15. “a” + (“b” + “c”) (“a” + “b”) + “c”

    =
  16. [1] + ([2] + [3]) ([1] + [2]) + [3]

    =
  17. + ( + ) = ( + ) +

  18. 0

  19. 1 + 0 1 =

  20. “hello” + “” “hello” =

  21. [1,2] + [] [1,2] =

  22. + =

  23. + ( + )

  24. (foldl + 0 [1,2,3]) 6 =

  25. (foldl + “” [“a”,”b”,”c”]) “abc” =

  26. (foldl + [] [[1],[2],[3]]) [1,2,3] =

  27. (foldl + [ , , ]) =

  28. …functions?

  29. ( + )() (()) =

  30. + ( + ℎ) ( + ) + ℎ =

  31. () =

  32. + =

  33. (foldl + [,,ℎ])() = ((ℎ()))

  34. + ( + )

  35. Monoids.

  36. None
  37. [ Arrays ] ( Lists ) { Sets }

  38. [ Arrays ] ( Lists ) { Sets } Collection[T]

  39. 0 | n

  40. (map f collection) Applies f to each element of in

    collection, if any. Returns a collection with the transformed elements, or an empty one if the original was empty.
  41. (map inc [1,2,3]) (map inc []) [2,3,4] []

  42. Some(t) None()

  43. Some(t) None() Option[T]

  44. 0 | 1

  45. (map f option) Applies f to the only element in

    option, if it’s there. Returns an option containing the transformed element or None.
  46. (map inc (some 1)) (some 2) (map inc (none)) (none)

  47. Future[T]

  48. eventually x?

  49. (map f future) Applies f to the element that the

    future may eventually yield, if it ever does. Returns a future that’ll yield either the transformed element or an error.
  50. (map inc (future 1)) (future 2) (map inc (future error))

    (future error)
  51. None
  52. Box[T]

  53. (map f ) Applies f to whatever is inside in

    the way sees fit. Returns the result in the same kind of .
  54. IN THE WAY IT SEES FIT.

  55. HOW MANY TIMES.

  56. EXACTLY WHEN.

  57. IF AT ALL. ?

  58. Encapsulation.

  59. (first []) (value (none)) Don’t reach into the box. (await

    (future (sleep)) nil? error? nil? error? blocks thread!
  60. Let the decide.

  61. Let the decide. Functors.

  62. [+] [1, 2, 3] [2, 3, 4] [1, 1, 1]

  63. (some +) (some 1) (some 2) (some 1)

  64. (fapply …) Applies whatever function is inside taking each _

    as arguments, assuming they are all the same kind of box, in the way the boxes see fit. f f
  65. (fapply Applies whatever function is inside _ as arguments, assuming

    they are all the same kind of box, in the way the boxes see fit. f f Applicative functors.
  66. None
  67. Composition.

  68. Box[T] (T -> Box[U])

  69. Collection[String] (String -> Collection[String])

  70. “foo.bar,foo.baz,foo.quux” (split “,”) [“foo.bar”,”foo.baz”,”foo.quux”] (map #(split “.” %)) [[“foo”, “bar”],

    [”foo”, “baz”], [”foo”, “quux”]]
  71. (flatmap f collection) Applies f to every element in collection,

    and then concatenates the results (presumably a nested collection) into a single flattened collection.
  72. “foo.bar,foo.baz,foo.quux” (split “,”) [“foo.bar”,”foo.baz”,”foo.quux”] (flatmap #(split “.”)) [“foo”, “bar”, ”foo”,

    “baz”, ”foo”, “quux”]
  73. Option[Address] (Address -> Option[StreetNumber])

  74. user (address) (some #<Address>) (map street-number) (some (some #<StreetNumber>))

  75. (flatmap f optional) Applies f to the element in optional

    if it’s there, then flattens the result (presumably a nested optional) a single flattened optional.
  76. user (address) (some #<Address>) (flatmap street-number) (some #<StreetNumber>)

  77. Future[Repo] (Repo -> Future[Followers])

  78. github-user (first-repo) (future repo1) (map followers) (future (future [follower1, follower2,

    …]))
  79. (flatmap f future) Applies f to the element eventually yielded

    by future, then flattens the result (presumably a nested future) a single flattened future.
  80. github-user (first-repo) (future repo1) (flatmap followers) (future [follower1, follower2, …])

  81. Box[T] (T -> Box[U]) (U -> Box[V]) (V -> Box[X])

  82. Applies f to whatever is inside in the way sees

    fit, then flattens the result (presumably a nested ) into a single flat of the same kind. (flatmap f )
  83. Box[T] (T -> Box[U]) (U -> Box[V]) (V -> Box[X])

    … Monads.
  84. None
  85. + ( + ) foldl

  86. map

  87. fapply f …

  88. flatmap

  89. None
  90. (and shout out to Jessica Kerr! @jessitron) Thank you. txustice

    txus Questions?