O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.
Ur Domain Haz 
Monoids? 
DDDx NYC 14 
cyrille martraire aka @cyriux
Can you tell 
the 
difference?
Monoid
Glass of beer
This talk is 
the answer
DDD
FP
(00)
A matter 
of Taste 
http://rosshirt.blogspot.fr/ 
The Code 
Gourmet 
(dedicated to @ziobrando)
Passionate 
developer 
PARIS 
Since 1999 
! 
@cyriux 
Cyrille Martraire
Paris Software 
Craftsmanship Community 
http://www.meetup.com/paris-software-craftsmanship/
TDD 
BDD 
DDD 
Legacy
WARNING 
The following show features 
no spectacular stunt, no live 
coding, only trivial Java code. 
You can re-create or...
a bit personal
Adopted 2005 
Still in love
...
What do DDD & FP 
have in common?
"My first encounter 
with FP concepts 
was from DDD"
FP = ?
http://www.jaider.net/archives/609-intro-to-functional-programming/ 
PURE
No State 
No War
So what do 
DDD & FP 
have in 
common?
Value Objects................................................................................................. 19
Learn one 
and get the other 
one for FREE!
DDD 
FP 
(OO)
NICE 
STYLE of code
DDD+FP 
= ?
Example 
PLZ?
MONOIDZ!
@cyriux
Closure 
Associativity 
Neutral Element
only 3 
numbers in 
programming
0, 1, MANY
Monoid: 
encapsulate 
diversity inside
Neutral Element 
0, 1, MANY 
Element 
Operation
Encapsulate 
special cases 
→ simple again
Battle against 
complexity
× × × …
× × × …
× × × …
Applied often: 
→ scalable process 
→ can grow to 
very complex 
complexity
Scalable in 
complexity
OO FTW! 
For a given interface I 
find myself doing 
often 0, 1, Many
NullObject 
Composite 
0, 1, MANY 
Implementation(s)
Identity Element 
= NullObject 
VatCalculation.NONE
Example 
PLZ!
Numbers 
int+int=int 
(3+5)+2=3+(5+2) 
0
Lists 
(.)+(.,.)=(.,.,.) 
(a︎)+(b︎,c)=(a︎, b)+(c) 
()
Strings 
"hello"+"world" 
"cy"+"ri"+"lle" 
""
look simplistic; 
the key to very 
complex behavior
The key to infinite 
scalability! 
(space)
The key to infinite 
incremental 
computing! 
(time)
Hadoop x Storm
Composeability
Monoids ☛ reduce
Monoids ☛ domain
Monoids ☛ domain
Monoids: 
typical FP
FP: 
Everything is a 
value
PROOF 
Therefore: 
Monoids are 
values!
VALUE OBJECTS
Immutable 
Equals by value
Value Object 
A DDD pattern 
to import FP-ish 
values in OO 
languages
Not Anemic
18 m 
+ 16 m 
= 34 m
Change -> new instance 
returns a new 
instance
SIDE-EFFECT 
-free 
FUNCTIONS
Immutability 
& 
Side-effect-free 
functions!
“Functional-First” style
“Functional-First” style 
90% 
Value Objects 
(Ok, gut feeling, I did’nt measure)
Money
(25, EUR) 
+ 
(30, EUR) 
= 
(55, EUR)
(25, EUR) 
+ 
(30, USD) 
= 
exception
Money 
<<ValueObject>> 
amount 
currency 
add(Money): Money
Cashflows 
(Payments)
(25, EUR, today) 
+ 
(30, EUR, today) 
= 
(55, EUR, today)
(25, EUR, today) 
+ 
(30, EUR, next day) 
= 
exception
CashFlow 
<<ValueObject>> 
amount 
currency 
date 
add(CashFlow):CashFlow
CLOSURE of 
OPERATION
Cashflows 
Sequences
(30, EUR, 21/03) 
(10, EUR, 20/03) 
(25, EUR, 21/03) 
(12, EUR, 22/03) 
(10, EUR, 20/03) 
(55, EUR, 21/03) 
(12, EUR, 22/0...
Cashflow Sequence 
+ 
Cashflow Sequence 
= 
Cashflow Sequence
Object 
Arithmetics
This is how domain 
experts THINK 
about it 
SAY 
SKETCH
Ranges
[1, 3] 
Union* 
[2, 4] 
= 
[1, 4] 
* for a bounding box-kind definition of union
[1, 3] 
Union 
[2, 4] 
= 
[1, 4] ][
Predicates
Filter 
AND 
Filter 
= 
Filter
Filter 
AND 
Filter 
= 
Filter Always 
True
Filter 
OR 
Filter 
= 
Filter
Filter 
OR 
Filter 
= 
Filter Always 
False
Grants
Read, Write, Execute 
! 
"most secure wins" 
! 
r + w = r 
w + x = w
Configuration 
Maps
Default Desk-specific 
Color BLUE 
Enable True 
Timeout 30 + 
= 
Color RED 
Enable False 
Timeout 25 
Desk FX 
Color RED 
...
Default Desk-specific 
Color BLUE 
Enable True 
Timeout 30 + 
= 
Color RED 
Enable False 
Timeout 25 
Desk FX 
Color RED 
...
Default Desk-specific 
Color BLUE 
Enable True 
Timeout 30 + 
Values are monoids too! 
= 
Color RED 
Enable False 
Timeout...
Color BLUE 
Enable True 
Timeout 30 + 
Neutral Element 
= 
Color NONE 
Enable False 
Timeout +∞ 
Desk ALL 
Color BLUE 
Ena...
Color BLUE 
Enable True 
Timeout 30 + 
Neutral Element 
= 
Color BLUE 
Enable True 
Timeout 30
Color BLUE 
Enable True 
Timeout 30 + 
Value Objects may be 
*BIG* object trees! 
(DOM) 
= 
Color BLUE 
Enable True 
Timeo...
Non-Linear Stuff 
(average, std dev, 
K-clustering, 
barycenters...)
Average + Average = 
WRONG
Average + Average 
Not Composeable!
avg = sum / count 
! 
avg = sum / count 
!
avg = sum / count 
! 
avg = sum / count 
! 
+
avg = sum / count 
! 
avg = sum / count 
! 
+ +
avg = sum / count 
+ 
= 
+ 
= 
! 
avg = sum / count 
! 
avg = sum / count
(sum, count) 
! 
+ 
= 
(sum, count) 
! 
(sum, count) 
Average
(sum, sum2, count) 
+ 
= 
! 
(sum, sum2, count) 
! 
(sum, sum2, count) 
Std deviation
Can model as a 
monoid even non-linear 
stuff!
MOAR MATHS PLZ!
Monoid several times... 
MailingList 
toString(): String 
union(MailingList): MailingList 
intersection(MailingList): Mail...
Space 
Vectors
average temperature! 
= t1.add(t2)! 
.scale(1/2)
Change -> new instance 
returns a new 
instance
Space Vector 
Temperature 
toCelsius(): Temperature 
toFarenheit(): Temperature 
add(Temperature): Temperature 
scale(doub...
Why 
is it useful?
(30, EUR, 21/03) 
(10, EUR, 20/03) 
(25, EUR, 21/03) 
(12, EUR, 22/03) 
(10, EUR, 20/03) 
(55, EUR, 21/03) 
(12, EUR, 22/0...
Cashflow Sequence 
+ 
Cashflow Sequence 
= 
Cashflow Sequence
This is how domain 
experts THINK 
about it 
SAY 
SKETCH
DECLARATIVE STYLE
Much less code 
Much less bugs
// without monoids 
PaymentsFees(...) 
PaymentsFeesWithOptions(...) 
PaymentsFeesWithInsuranceAndOptions(...) 
PaymentsFee...
// without monoids 
PaymentsFees(...) 
PaymentsFeesWithOptions(...) 
PaymentsFeesWithInsuranceAndOptions(...) 
PaymentsFee...
// with monoids 
fees(...) : Payments 
options(...) : Payments 
insurance(...) : Payments 
! 
Payments.add(Payments) : 
Pa...
Very easy to test 
side-effect-free 
operation 
input output
Much less stuff to 
learn
1 interface 
to rule them all
Cashflow Sequence 
+ 
Cashflow Sequence 
= 
Cashflow Sequence
ESTABLISHED FORMALISMS
Monoid/Vector 
Spaces/Cyclic Group
Literature 
Documented
@annotations 
MailingList 
@Monoid(neutral="emptyList") 
intersection(MailingList): MailingList 
! 
emptyList(): MailingLi...
LIVING DOCUMENTATION
Write code that 
tells the business 
domain stories
generic naming 
MailingList 
@Monoid(neutral="emptyList") 
intersection(MailingList): MailingList 
! 
emptyList(): Mailing...
Domain-Specific naming 
MailingList 
@Monoid(neutral="nobody") 
overlapping(MailingList): MailingList 
! 
nobody(): Mailin...
SELF-EXPLAINING 
VALUES
We Want: 
Traceability 
of processing
No worry!
Just enrich our 
types
Just enrich our 
types
Just enrich our 
types 
! 
label field
Monad-ish 
No logging needed 
Each value stores 
its history
http://stuartcook.files.wordpress.com/2010/11/happy-monkey.jpg
In Closing
Composeability 
Composeability 
Composeability 
Composeability 
Composeability 
Composeability
Invest time: 
Learn DDD, and get 
free FP exposure
A paradox: 
FP influence helps 
craft better Object- 
Oriented code!
So simple 
most people 
have no 
appreciation 
of that!
Monoids 
are good: 
Eat Them!
LOOK 4 Ur 
DOMAIN 
MONOIDZ!
Also learn other 
maths structures
Wikipedia is 
your friend!
DDD+FP 
=
http://wadler.blogspot.fr/2008/04/functional-programming-is-beautiful.html
TDD 
Taste-Driven 
Development 
@cyriux
Questions? Did you try 
similar things too? 
Let’s discuss! 
@cyriux
Follow me @cyriux 
! 
Slides: slideshare.net/cyriux 
Blog: cyrille.martraire.com 
! 
In Paris? Join !
Merci
Abstractions in 
the small that 
compose at large
Ur Domain Haz Monoids DDDx NYC 2014
Ur Domain Haz Monoids DDDx NYC 2014
Ur Domain Haz Monoids DDDx NYC 2014
Ur Domain Haz Monoids DDDx NYC 2014
Ur Domain Haz Monoids DDDx NYC 2014
Ur Domain Haz Monoids DDDx NYC 2014
Ur Domain Haz Monoids DDDx NYC 2014
Ur Domain Haz Monoids DDDx NYC 2014
Ur Domain Haz Monoids DDDx NYC 2014
Ur Domain Haz Monoids DDDx NYC 2014
Ur Domain Haz Monoids DDDx NYC 2014
Ur Domain Haz Monoids DDDx NYC 2014
Ur Domain Haz Monoids DDDx NYC 2014
Ur Domain Haz Monoids DDDx NYC 2014
Próximos SlideShares
Carregando em…5
×

Ur Domain Haz Monoids DDDx NYC 2014

3.667 visualizações

Publicada em

You probably can't imagine that Monoids (not monads) are so simple maths creatures that you can understand them in just a few minutes.

You probably can't imagine that Monoids (not monads) are so simple maths creatures that you can understand them in just a few minutes.

But you probably don't imagine either that they can help you craft elegant and powerful domain models that scale very well.

Through various examples, we will have a closer look at monoids used for domain modeling in a style that mixes the best of DDD and FP. Even in languages like Java or C#, this talk will influence your coding style forever!

'More entertaining and educational explanation of Monoids I've heard' - Martin Thompson, DDD exchange London 2014.

See more at http://skillsmatter.com/conferences/1880-ddd-exchange-nyc-2014#program

Publicada em: Software

Ur Domain Haz Monoids DDDx NYC 2014

  1. 1. Ur Domain Haz Monoids? DDDx NYC 14 cyrille martraire aka @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. ...
  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 STYLE of 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. Neutral Element 0, 1, MANY 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 very complex complexity
  42. 42. Scalable in complexity
  43. 43. OO FTW! For a given interface I find myself doing often 0, 1, Many
  44. 44. NullObject Composite 0, 1, MANY Implementation(s)
  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. Monoids ☛ reduce
  56. 56. Monoids ☛ domain
  57. 57. Monoids ☛ domain
  58. 58. Monoids: typical FP
  59. 59. FP: Everything is a value
  60. 60. PROOF Therefore: Monoids are values!
  61. 61. VALUE OBJECTS
  62. 62. Immutable Equals by value
  63. 63. Value Object A DDD pattern to import FP-ish values in OO languages
  64. 64. Not Anemic
  65. 65. 18 m + 16 m = 34 m
  66. 66. Change -> new instance returns a new instance
  67. 67. SIDE-EFFECT -free FUNCTIONS
  68. 68. Immutability & Side-effect-free functions!
  69. 69. “Functional-First” style
  70. 70. “Functional-First” style 90% Value Objects (Ok, gut feeling, I did’nt measure)
  71. 71. Money
  72. 72. (25, EUR) + (30, EUR) = (55, EUR)
  73. 73. (25, EUR) + (30, USD) = exception
  74. 74. Money <<ValueObject>> amount currency add(Money): Money
  75. 75. Cashflows (Payments)
  76. 76. (25, EUR, today) + (30, EUR, today) = (55, EUR, today)
  77. 77. (25, EUR, today) + (30, EUR, next day) = exception
  78. 78. CashFlow <<ValueObject>> amount currency date add(CashFlow):CashFlow
  79. 79. CLOSURE of OPERATION
  80. 80. Cashflows Sequences
  81. 81. (30, EUR, 21/03) (10, EUR, 20/03) (25, EUR, 21/03) (12, EUR, 22/03) (10, EUR, 20/03) (55, EUR, 21/03) (12, EUR, 22/03) + =
  82. 82. Cashflow Sequence + Cashflow Sequence = Cashflow Sequence
  83. 83. Object Arithmetics
  84. 84. This is how domain experts THINK about it SAY SKETCH
  85. 85. Ranges
  86. 86. [1, 3] Union* [2, 4] = [1, 4] * for a bounding box-kind definition of union
  87. 87. [1, 3] Union [2, 4] = [1, 4] ][
  88. 88. Predicates
  89. 89. Filter AND Filter = Filter
  90. 90. Filter AND Filter = Filter Always True
  91. 91. Filter OR Filter = Filter
  92. 92. Filter OR Filter = Filter Always False
  93. 93. Grants
  94. 94. Read, Write, Execute ! "most secure wins" ! r + w = r w + x = w
  95. 95. Configuration Maps
  96. 96. Default Desk-specific Color BLUE Enable True Timeout 30 + = Color RED Enable False Timeout 25 Desk FX Color RED Enable True Timeout 25 Desk FX
  97. 97. Default Desk-specific Color BLUE Enable True Timeout 30 + = Color RED Enable False Timeout 25 Desk FX Color RED Enable True Timeout 25 Desk FX overwrite logical OR min
  98. 98. Default Desk-specific Color BLUE Enable True Timeout 30 + Values are monoids too! = Color RED Enable False Timeout 25 Desk FX Color RED Enable True Timeout 25 Desk FX overwrite logical OR min
  99. 99. Color BLUE Enable True Timeout 30 + Neutral Element = Color NONE Enable False Timeout +∞ Desk ALL Color BLUE Enable True Timeout 30
  100. 100. Color BLUE Enable True Timeout 30 + Neutral Element = Color BLUE Enable True Timeout 30
  101. 101. Color BLUE Enable True Timeout 30 + Value Objects may be *BIG* object trees! (DOM) = Color BLUE Enable True Timeout 30
  102. 102. Non-Linear Stuff (average, std dev, K-clustering, barycenters...)
  103. 103. Average + Average = WRONG
  104. 104. Average + Average Not Composeable!
  105. 105. avg = sum / count ! avg = sum / count !
  106. 106. avg = sum / count ! avg = sum / count ! +
  107. 107. avg = sum / count ! avg = sum / count ! + +
  108. 108. avg = sum / count + = + = ! avg = sum / count ! avg = sum / count
  109. 109. (sum, count) ! + = (sum, count) ! (sum, count) Average
  110. 110. (sum, sum2, count) + = ! (sum, sum2, count) ! (sum, sum2, count) Std deviation
  111. 111. Can model as a monoid even non-linear stuff!
  112. 112. MOAR MATHS PLZ!
  113. 113. Monoid several times... MailingList toString(): String union(MailingList): MailingList intersection(MailingList): MailingList nobody(): MailingList everybody(): MailingList
  114. 114. Space Vectors
  115. 115. average temperature! = t1.add(t2)! .scale(1/2)
  116. 116. Change -> new instance returns a new instance
  117. 117. Space Vector Temperature toCelsius(): Temperature toFarenheit(): Temperature add(Temperature): Temperature scale(double): Temperature
  118. 118. Why is it useful?
  119. 119. (30, EUR, 21/03) (10, EUR, 20/03) (25, EUR, 21/03) (12, EUR, 22/03) (10, EUR, 20/03) (55, EUR, 21/03) (12, EUR, 22/03) + =
  120. 120. Cashflow Sequence + Cashflow Sequence = Cashflow Sequence
  121. 121. This is how domain experts THINK about it SAY SKETCH
  122. 122. DECLARATIVE STYLE
  123. 123. Much less code Much less bugs
  124. 124. // without monoids PaymentsFees(...) PaymentsFeesWithOptions(...) PaymentsFeesWithInsuranceAndOptions(...) PaymentsFeesWithInsurance(...) NoFeesButInsurance(...) ... ! // with monoids fees(...) : Payments options(...) : Payments insurance(...) : Payments ! Payments.add(Payments) : Payments
  125. 125. // without monoids PaymentsFees(...) PaymentsFeesWithOptions(...) PaymentsFeesWithInsuranceAndOptions(...) PaymentsFeesWithInsurance(...) NoFeesButInsurance(...) ...
  126. 126. // with monoids fees(...) : Payments options(...) : Payments insurance(...) : Payments ! Payments.add(Payments) : Payments
  127. 127. Very easy to test side-effect-free operation input output
  128. 128. Much less stuff to learn
  129. 129. 1 interface to rule them all
  130. 130. Cashflow Sequence + Cashflow Sequence = Cashflow Sequence
  131. 131. ESTABLISHED FORMALISMS
  132. 132. Monoid/Vector Spaces/Cyclic Group
  133. 133. Literature Documented
  134. 134. @annotations MailingList @Monoid(neutral="emptyList") intersection(MailingList): MailingList ! emptyList(): MailingList
  135. 135. LIVING DOCUMENTATION
  136. 136. Write code that tells the business domain stories
  137. 137. generic naming MailingList @Monoid(neutral="emptyList") intersection(MailingList): MailingList ! emptyList(): MailingList
  138. 138. Domain-Specific naming MailingList @Monoid(neutral="nobody") overlapping(MailingList): MailingList ! nobody(): MailingList
  139. 139. SELF-EXPLAINING VALUES
  140. 140. We Want: Traceability of processing
  141. 141. No worry!
  142. 142. Just enrich our types
  143. 143. Just enrich our types
  144. 144. Just enrich our types ! label field
  145. 145. Monad-ish No logging needed Each value stores its history
  146. 146. http://stuartcook.files.wordpress.com/2010/11/happy-monkey.jpg
  147. 147. In Closing
  148. 148. Composeability Composeability Composeability Composeability Composeability Composeability
  149. 149. Invest time: Learn DDD, and get free FP exposure
  150. 150. A paradox: FP influence helps craft better Object- Oriented code!
  151. 151. So simple most people have no appreciation of that!
  152. 152. Monoids are good: Eat Them!
  153. 153. LOOK 4 Ur DOMAIN MONOIDZ!
  154. 154. Also learn other maths structures
  155. 155. Wikipedia is your friend!
  156. 156. DDD+FP =
  157. 157. http://wadler.blogspot.fr/2008/04/functional-programming-is-beautiful.html
  158. 158. TDD Taste-Driven Development @cyriux
  159. 159. Questions? Did you try similar things too? Let’s discuss! @cyriux
  160. 160. Follow me @cyriux ! Slides: slideshare.net/cyriux Blog: cyrille.martraire.com ! In Paris? Join !
  161. 161. Merci
  162. 162. Abstractions in the small that compose at large

×