@@ -539,7 +539,7 @@ randIter.next() // Each execution gives a random value, expression is evaluated
539539
540540## Monoid
541541
542- An object with a function that "combines" that object with another of the same type.
542+ An object with a function that "combines" that object with another of the same type (semigroup) which has an "identity" value .
543543
544544One simple monoid is the addition of numbers:
545545
@@ -548,11 +548,13 @@ One simple monoid is the addition of numbers:
548548```
549549In this case number is the object and ` + ` is the function.
550550
551- An "identity" value must also exist that when combined with a value doesn't change it .
551+ When any value is combined with the "identity" value the result must be the original value. The identity must also be commutative .
552552
553553The identity value for addition is ` 0 ` .
554554``` js
5555551 + 0 // 1
556+ 0 + 1 // 1
557+ 1 + 0 === 0 + 1
556558```
557559
558560It's also required that the grouping of operations will not affect the result (associativity):
@@ -573,15 +575,10 @@ The identity value is empty array `[]`
573575;[1 , 2 ].concat ([]) // [1, 2]
574576```
575577
576- If identity and compose functions are provided, functions themselves form a monoid:
578+ As a counterexample, subtraction does not form a monoid because there is no commutative identity value :
577579
578580``` js
579- const identity = (a ) => a
580- const compose = (f , g ) => (x ) => f (g (x))
581- ```
582- ` foo ` is any function that takes one argument.
583- ```
584- compose(foo, identity) ≍ compose(identity, foo) ≍ foo
581+ 0 - 4 === 4 - 0 // false
585582```
586583
587584## Monad
0 commit comments