Skip to content

Commit

Permalink
Update readme.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Jethro Larson authored Sep 23, 2020
1 parent e5918a3 commit b33ace0
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ randIter.next() // Each execution gives a random value, expression is evaluated

## Monoid

An object with a function that "combines" that object with another of the same type.
An object with a function that "combines" that object with another of the same type (semigroup) which has an "identity" value.

One simple monoid is the addition of numbers:

Expand All @@ -548,11 +548,13 @@ One simple monoid is the addition of numbers:
```
In this case number is the object and `+` is the function.

An "identity" value must also exist that when combined with a value doesn't change it.
When any value is combined with the "identity" value the result must be the original value. The identity must also be commutative.

The identity value for addition is `0`.
```js
1 + 0 // 1
0 + 1 // 1
1 + 0 === 0 + 1
```

It's also required that the grouping of operations will not affect the result (associativity):
Expand All @@ -573,15 +575,10 @@ The identity value is empty array `[]`
;[1, 2].concat([]) // [1, 2]
```

If identity and compose functions are provided, functions themselves form a monoid:
As a counterexample, subtraction does not form a monoid because there is no commutative identity value:

```js
const identity = (a) => a
const compose = (f, g) => (x) => f(g(x))
```
`foo` is any function that takes one argument.
```
compose(foo, identity) ≍ compose(identity, foo) ≍ foo
0 - 4 === 4 - 0 // false
```

## Monad
Expand Down

0 comments on commit b33ace0

Please sign in to comment.