mirror of
https://github.com/rjNemo/functional-programming-jargon
synced 2026-06-11 04:56:40 +00:00
Merge pull request #27 from matthieubulte/patch-1
Added functor laws and example to the functor section
This commit is contained in:
commit
296ad67652
1 changed files with 27 additions and 1 deletions
28
readme.md
28
readme.md
|
|
@ -159,13 +159,39 @@ Points-free function definitions look just like normal assignments without `func
|
||||||
|
|
||||||
## Functor
|
## Functor
|
||||||
|
|
||||||
> An object with a `map` function. `Map` runs a function on values in an object and returns a new object.
|
> An object with a `map` function that adhere to certains rules. `Map` runs a function on values in an object and returns a new object.
|
||||||
|
|
||||||
Simplest functor in javascript is an `Array`
|
Simplest functor in javascript is an `Array`
|
||||||
|
|
||||||
```js
|
```js
|
||||||
[2,3,4].map( n => n * 2 ); // [4,6,8]
|
[2,3,4].map( n => n * 2 ); // [4,6,8]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Let `func` be an object implementing a `map` function, and `f`, `g` be arbitrary functions, then `func` is said to be a functor if the map function adheres to the following rules:
|
||||||
|
|
||||||
|
```js
|
||||||
|
func.map(x => x) == func
|
||||||
|
```
|
||||||
|
|
||||||
|
and
|
||||||
|
|
||||||
|
```js
|
||||||
|
func.map(x => f(g(x))) == func.map(g).map(f)
|
||||||
|
```
|
||||||
|
|
||||||
|
We can now see that `Array` is a functor because it adheres to the functor rules!
|
||||||
|
```js
|
||||||
|
[1, 2, 3].map(x => x); // = [1, 2, 3]
|
||||||
|
```
|
||||||
|
|
||||||
|
and
|
||||||
|
```js
|
||||||
|
let f = x => x + 1;
|
||||||
|
let g = x => x * 2;
|
||||||
|
|
||||||
|
[1, 2, 3].map(x => f(g(x))); // = [3, 5, 7]
|
||||||
|
[1, 2, 3].map(g).map(f); // = [3, 5, 7]
|
||||||
|
```
|
||||||
---
|
---
|
||||||
|
|
||||||
## Pointed Functor
|
## Pointed Functor
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue