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