Successfully reported this slideshow.
Your SlideShare is downloading. ×

Cyrille Martraire: Monoids, Monoids Everywhere! at I T.A.K.E. Unconference 2015

Ad

Monoids,!
Monoids everywhere
cyrille martraire!
@cyriux

Ad

Can you tell
the
difference?

Ad

Monoid

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Ad

Upcoming SlideShare
PythonArtCode
PythonArtCode
Loading in …3
×

Check these out next

1 of 174 Ad
1 of 174 Ad

Cyrille Martraire: Monoids, Monoids Everywhere! at I T.A.K.E. Unconference 2015

Download to read offline

Cyrille Martraire: Monoids, Monoids Everywhere! at I T.A.K.E. Unconference 2015

Cyrille Martraire: Monoids, Monoids Everywhere! at I T.A.K.E. Unconference 2015

More Related Content

Similar to Cyrille Martraire: Monoids, Monoids Everywhere! at I T.A.K.E. Unconference 2015 (20)

More from Mozaic Works (20)

Cyrille Martraire: Monoids, Monoids Everywhere! at I T.A.K.E. Unconference 2015

  1. 1. Monoids,! Monoids everywhere cyrille martraire! @cyriux
  2. 2. Can you tell the difference?
  3. 3. Monoid
  4. 4. Glass of beer
  5. 5. This talk is the answer
  6. 6. DDD
  7. 7. FP
  8. 8. (00)
  9. 9. A matter of Taste http://rosshirt.blogspot.fr/ The Code Gourmet (dedicated to @ziobrando)
  10. 10. Passionate developer PARIS Since 1999 ! @cyriux Cyrille Martraire
  11. 11. Paris Software Craftsmanship Community http://www.meetup.com/paris-software-craftsmanship/
  12. 12. TDD BDD DDD Legacy
  13. 13. WARNING The following show features no spectacular stunt, no live coding, only trivial Java code. You can re-create or re-enact at home with no danger.
  14. 14. a bit personal
  15. 15. Adopted 2005 Still in love
  16. 16. a question
  17. 17. What do DDD & FP have in common?
  18. 18. "My first encounter with FP concepts was from DDD"
  19. 19. FP = ?
  20. 20. http://www.jaider.net/archives/609-intro-to-functional-programming/ PURE
  21. 21. No State No War
  22. 22. So what do DDD & FP have in common?
  23. 23. Value Objects................................................................................................. 19
  24. 24. Learn one and get the other one for FREE!
  25. 25. DDD FP (OO)
  26. 26. NICE STYLEof code
  27. 27. DDD+FP = ?
  28. 28. Example PLZ?
  29. 29. MONOIDZ!
  30. 30. @cyriux
  31. 31. Closure Associativity Neutral Element
  32. 32. only 3 numbers in programming
  33. 33. 0, 1, MANY
  34. 34. Monoid: encapsulate diversity inside
  35. 35. 0, 1, MANY Neutral Element Element Operation
  36. 36. Encapsulate special cases → simple again
  37. 37. Battle against complexity
  38. 38. × × × …
  39. 39. × × × …
  40. 40. × × × …
  41. 41. Applied often: → scalable process → can grow to high complexity
  42. 42. Scalable in complexity
  43. 43. For a given interface I find myself doing often 0, 1, Many OO FTW!
  44. 44. 0, 1, MANY NullObject Implementation(s) Composite
  45. 45. Identity Element = NullObject VatCalculation.NONE
  46. 46. Example PLZ!
  47. 47. Numbers int+int=int (3+5)+2=3+(5+2) 0
  48. 48. Lists (.)+(.,.)=(.,.,.) (a︎)+(b︎,c)=(a︎, b)+(c) ()
  49. 49. Strings "hello"+"world" "cy"+"ri"+"lle" ""
  50. 50. look simplistic; the key to very complex behavior
  51. 51. The key to infinite scalability! (space)
  52. 52. The key to infinite incremental computing! (time)
  53. 53. Hadoop x Storm
  54. 54. Composeability
  55. 55. Abstractions in the small that compose at large
  56. 56. Monoids ☛ reduce
  57. 57. Monoids ☛ domain
  58. 58. Monoids ☛ domain
  59. 59. Monoids: typical FP
  60. 60. FP: Everything is a value
  61. 61. Therefore: Monoids are values! PROOF
  62. 62. VALUE OBJECTS
  63. 63. Immutable Equals by value
  64. 64. Value Object A DDD pattern to import FP-ish values in OO languages
  65. 65. Not Anemic
  66. 66. 18 m + 16 m = 34 m
  67. 67. returns  a  new   instance Change -> new instance
  68. 68. SIDE-EFFECT -free FUNCTIONS
  69. 69. Immutability & Side-effect-free functions!
  70. 70. “Functional-First” style
  71. 71. 90% “Functional-First” style Value Objects (Ok, gut feeling, I did’nt measure)
  72. 72. Money
  73. 73. (25, EUR) + (30, EUR) = (55, EUR)
  74. 74. (25, EUR) + (30, USD) = exception
  75. 75. Money amount currency add(Money): Money <<ValueObject>>
  76. 76. Cashflows (Payments)
  77. 77. (25, EUR, today) + (30, EUR, today) = (55, EUR, today)
  78. 78. (25, EUR, today) + (30, EUR, next day) = exception
  79. 79. CashFlow amount currency date add(CashFlow):CashFlow <<ValueObject>>
  80. 80. CLOSUREof OPERATION
  81. 81. Cashflows Sequences
  82. 82. (10, EUR, 20/03) (30, EUR, 21/03) (25, EUR, 21/03) (12, EUR, 22/03) (10, EUR, 20/03) (55, EUR, 21/03) (12, EUR, 22/03) + =
  83. 83. Cashflow Sequence + Cashflow Sequence = Cashflow Sequence
  84. 84. Object Arithmetics
  85. 85. This is how domain experts THINK about it SAY SKETCH
  86. 86. Ranges
  87. 87. [1, 3] Union* [2, 4] = [1, 4] * for a bounding box-kind definition of union
  88. 88. [1, 3] Union [2, 4] = [1, 4] ][
  89. 89. Predicates
  90. 90. Filter AND Filter = Filter
  91. 91. Filter AND Filter = Filter Always True
  92. 92. Filter OR Filter = Filter
  93. 93. Filter OR Filter = Filter Always False
  94. 94. Grants
  95. 95. Read, Write, Execute ! "most secure wins" ! r + w = r w + x = w
  96. 96. Configuration Maps
  97. 97. Color BLUE Enable True Timeout 30 + = Color RED Enable False Timeout 25 Desk FX Color RED Enable True Timeout 25 Desk FX Default Desk-specific
  98. 98. Color BLUE Enable True Timeout 30 + = Color RED Enable False Timeout 25 Desk FX Color RED Enable True Timeout 25 Desk FX Default Desk-specific overw rite logical OR m in
  99. 99. Color BLUE Enable True Timeout 30 + = Color RED Enable False Timeout 25 Desk FX Color RED Enable True Timeout 25 Desk FX Default Desk-specific overw rite logical OR m in Values are monoids too!
  100. 100. Color BLUE Enable True Timeout 30 + = Color BLUE Enable True Timeout 30 Color NONE Enable False Timeout +∞ Desk ALL Neutral Element
  101. 101. Color BLUE Enable True Timeout 30 + = Color BLUE Enable True Timeout 30 Neutral Element
  102. 102. Color BLUE Enable True Timeout 30 + = Color BLUE Enable True Timeout 30 Value Objects may be *BIG* object trees! (DOM)
  103. 103. Non-Linear Stuff (average, std dev, K-clustering, barycenters...)
  104. 104. Average + Average = WRONG
  105. 105. Average + Average Not Composeable!
  106. 106. avg = sum / count ! avg = sum / count !
  107. 107. avg = sum / count ! avg = sum / count ! +
  108. 108. avg = sum / count ! avg = sum / count ! + +
  109. 109. avg = sum / count ! avg = sum / count ! avg = sum / count + = + =
  110. 110. (sum, count) ! (sum, count) ! (sum, count) + = Average
  111. 111. (sum, sum2, count) ! (sum, sum2, count) ! (sum, sum2, count) + = Std deviation
  112. 112. Can model as a monoid even non- linear stuff!
  113. 113. MOAR MATHS PLZ!
  114. 114. Monoid several times... toString(): String union(MailingList): MailingList intersection(MailingList): Mailing nobody(): MailingList everybody(): MailingList MailingList
  115. 115. Space Vectors
  116. 116. average temperature! = t1.add(t2)! .scale(1/2)
  117. 117. returns  a  new   instance Change -> new instance
  118. 118. Space Vector toCelsius(): Temperature toFarenheit(): Temperature add(Temperature): Temperature scale(double): Temperature Temperature
  119. 119. Why is it useful?
  120. 120. (10, EUR, 20/03) (30, EUR, 21/03) (25, EUR, 21/03) (12, EUR, 22/03) (10, EUR, 20/03) (55, EUR, 21/03) (12, EUR, 22/03) + =
  121. 121. Cashflow Sequence + Cashflow Sequence = Cashflow Sequence
  122. 122. This is how domain experts THINK about it SAY SKETCH
  123. 123. DECLARATIVE STYLE
  124. 124. Much less code Much less bugs
  125. 125. // without monoids PaymentsFees(...) PaymentsFeesWithOptions(...) PaymentsFeesWithInsuranceAndO PaymentsFeesWithInsurance(... NoFeesButInsurance(...) ...
  126. 126. // with monoids fees(...) : Payments options(...) : Payments insurance(...) : Payments ! Payments.add(Payments) : Payments
  127. 127. side-effect- free operation Very easy to test input output
  128. 128. Much less stuff to learn
  129. 129. Cashflow Sequence + Cashflow Sequence = Cashflow Sequence
  130. 130. Monoid/Vector Spaces/Cyclic Group
  131. 131. Literature Documented
  132. 132. ESTABLISHED FORMALISMS
  133. 133. @annotations @Monoid(neutral="emptyList") intersection(MailingList): MailingList ! emptyList(): MailingList MailingList
  134. 134. LIVINGDOCUMENTATION
  135. 135. Write code that tells the business domain stories
  136. 136. generic naming @Monoid(neutral="emptyList") intersection(MailingList): MailingList ! emptyList(): MailingList MailingList
  137. 137. Domain-Specific naming @Monoid(neutral="nobody") overlapping(MailingList): MailingList ! nobody(): MailingList MailingList
  138. 138. SELF- EXPLAINING VALUES
  139. 139. WeWant: Traceability of processing
  140. 140. No worry!
  141. 141. Just enrich our types
  142. 142. Just enrich our types
  143. 143. Just enrich our types ! label field
  144. 144. Monad-ish No logging needed Each value stores its history
  145. 145. http://stuartcook.files.wordpress.com/2010/11/happy-monkey.jpg
  146. 146. In Closing
  147. 147. Composeability Composeability Composeability Composeability Composeability Composeability
  148. 148. Invest time: Learn DDD, and get free FP exposure
  149. 149. A paradox: FP influence helps craft better Object- Oriented code!
  150. 150. So simple, most people have no appreciation of that!
  151. 151. Monoids are good: Eat Them!
  152. 152. LOOK 4 Ur DOMAIN MONOIDZ!
  153. 153. Also learn other maths structures
  154. 154. Wikipedia is your friend!
  155. 155. DDD+FP =
  156. 156. http://wadler.blogspot.fr/2008/04/functional-programming-is-beautiful.html
  157. 157. Taste-Driven Development TDD @cyriux
  158. 158. Questions? Did you try similar things too? Let’s discuss! @cyriux
  159. 159. Follow me @cyriux ! Slides: slideshare.net/cyriux Blog: cyrille.martraire.com ! In Paris? Join !
  160. 160. Merci

×