From dd949e7230c86f170e3492c618ff43bda8137509 Mon Sep 17 00:00:00 2001 From: Jethro Larson Date: Thu, 28 Jul 2022 13:11:48 -0700 Subject: [PATCH 1/2] =?UTF-8?q?Eliminate=20usage=20of=20=E2=89=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #135 --- readme.md | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/readme.md b/readme.md index 4ed317a..2258f10 100644 --- a/readme.md +++ b/readme.md @@ -450,17 +450,27 @@ Object whose `chain` doesn't transform the contents. See [Monad](#monad) An object that implements a `map` function that takes a function which is run on the contents of that object. A functor must adhere to two rules: __Preserves identity__ -``` -object.map(x => x) ≍ object -``` + +```js +object.map(x => x) +``` + +is equivalent to just `object`. + __Composable__ +```js +object.map(compose(f, g)) ``` -object.map(compose(f, g)) ≍ object.map(g).map(f) + +is equivalent to + +```js + object.map(g).map(f) ``` -(`f`, `g` are arbitrary functions) +(`f`, `g` are arbitrary composable functions) A common functor in JavaScript is `Array` since it abides to the two functor rules: From 60b1b6ffa82c8ee54307d00f6300420561157730 Mon Sep 17 00:00:00 2001 From: Jethro Larson Date: Fri, 29 Jul 2022 10:37:43 -0700 Subject: [PATCH 2/2] change functor example to use Option implementation --- readme.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/readme.md b/readme.md index 2258f10..9a1b9bf 100644 --- a/readme.md +++ b/readme.md @@ -461,21 +461,21 @@ is equivalent to just `object`. __Composable__ ```js -object.map(compose(f, g)) +object.map(x => g(f(x))) ``` is equivalent to ```js - object.map(g).map(f) + object.map(f).map(g) ``` (`f`, `g` are arbitrary composable functions) -A common functor in JavaScript is `Array` since it abides to the two functor rules: +The reference implementation of [Option](#option) is a functor as it satisfies the rules: ```js -;[1, 2, 3].map(x => x) // = [1, 2, 3] +some(1).map(x => x) // = some(1) ``` and @@ -484,8 +484,8 @@ and const f = x => x + 1 const 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] +some(1).map(x => g(f(x))) // = some(3) +some(1).map(f).map(g) // = some(3) ``` ## Pointed Functor