mirror of
https://github.com/rjNemo/functional-programming-jargon
synced 2026-06-06 02:26:43 +00:00
Added functor laws and example to the functor section
This commit is contained in:
parent
1d35202348
commit
de552b5a2d
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
|
||||
|
||||
> 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`
|
||||
|
||||
```js
|
||||
[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
|
||||
|
|
|
|||
Loading…
Reference in a new issue