From da2cca182d7175cf65878ac53042ea43e6bc00bf Mon Sep 17 00:00:00 2001 From: Jethro Larson Date: Wed, 27 Jul 2016 00:53:47 -0700 Subject: [PATCH] Used Array.of for pointed functor definition #77 (#86) Also adjusted monoid description until my kid could understand it. --- readme.md | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/readme.md b/readme.md index cf37da2..c3876a2 100644 --- a/readme.md +++ b/readme.md @@ -305,14 +305,12 @@ const g = x => x * 2; ``` ## Pointed Functor -A functor with an `of` function that puts _any_ single value into that functor. +An object with an `of` function that puts _any_ single value into it. -Array Implementation: +ES2015 adds `Array.of` making arrays a pointed functor. ```js -Array.prototype.of = (v) => [v]; - -[].of(1) // [1] +Array.of(1) // [1] ``` ## Lift @@ -377,29 +375,29 @@ randIter.next(); // Each execution gives a random value, expression is evaluated ## 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. +An object with a function that "combines" that object with another of the same type. -One very simple monoid is numbers and addition: +One simple monoid is the addition of numbers: ```js 1 + 1; // 2 ``` +In this case number is the object and `+` is the function. -The data type is number and the function is `+`, the addition of two numbers. +An "identity" value must also exist that when combined with a value doesn't change it. +The identity value for addition is `0`. ```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 grouping of operations will not affect the result: +It's also required that the grouping of operations will not affect the result (associativity): ```js 1 + (2 + 3) === (1 + 2) + 3; // true ``` -Array concatenation can also be said to be a monoid: +Array concatenation also forms a monoid: ```js [1, 2].concat([3, 4]); // [1, 2, 3, 4]