mirror of
https://github.com/rjNemo/functional-programming-jargon
synced 2026-06-11 21:16:43 +00:00
commit
50c6ed4e09
1 changed files with 34 additions and 0 deletions
34
readme.md
34
readme.md
|
|
@ -166,6 +166,40 @@ randIter.next(); // Each exectuion gives a random value, expression is evluated
|
||||||
|
|
||||||
## Monoid
|
## Monoid
|
||||||
|
|
||||||
|
> A monoid is some data type and a two parameter function that "combines" two values of the type, where an identity value that does not affect the result of the function also exists.
|
||||||
|
|
||||||
|
The simplest monoid is numbers and addition:
|
||||||
|
|
||||||
|
```js
|
||||||
|
1 + 1; // 2
|
||||||
|
```
|
||||||
|
|
||||||
|
The data type is number and the function is `+`, the addition of two numbers.
|
||||||
|
|
||||||
|
```js
|
||||||
|
1 + 0; // 1
|
||||||
|
```
|
||||||
|
|
||||||
|
The identity value is `0` - adding `0` to any number will not change it.
|
||||||
|
|
||||||
|
For something to be a monoid, it's also required that the order of operations will not affect the result:
|
||||||
|
|
||||||
|
```js
|
||||||
|
1 + (2 + 3) == (1 + 2) + 3; // true
|
||||||
|
```
|
||||||
|
|
||||||
|
Array concatenation can also be said to be a monoid:
|
||||||
|
|
||||||
|
```js
|
||||||
|
[1, 2].concat([3, 4]); // [1, 2, 3, 4]
|
||||||
|
```
|
||||||
|
|
||||||
|
The identity value is empty array `[]`
|
||||||
|
|
||||||
|
```js
|
||||||
|
[1, 2].concat([]); // [1, 2]
|
||||||
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Monad
|
## Monad
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue