From f0c1e46d1fcff80386fc3ab0994edead8bcf15a4 Mon Sep 17 00:00:00 2001 From: Jethro Larson Date: Mon, 18 Jan 2016 19:06:25 -0800 Subject: [PATCH 1/3] Update readme.md Added Applicative functor, chain, equational reasoning. Added a line to monad. --- readme.md | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index df9be4a..686b5d9 100644 --- a/readme.md +++ b/readme.md @@ -166,6 +166,15 @@ Simplest functor in javascript is an `Array` ``` --- +## Lift + +> A function which puts a function into a container and applies that function to its next argument(s). Map is a lift over a one-argument function, but the same principal can be used to combine multiple functors. + +```js +lift(n => n * 2)([2,3,4]); // [4,6,8] +lift((a, b) => a * b)([1, 2], [3]); // [3, 6] +``` + ## Referential Transparency > An expression that can be replaced with its value without changing the @@ -184,6 +193,8 @@ referential transparent. ## Equational Reasoning +> When an application is composed of expressions and devoid of side effects, truths about the system can be derived from the parts. + --- ## Lazy evaluation @@ -243,7 +254,7 @@ The identity value is empty array `[]` ## Monad -> A monad is an abstraction that provides an interface for executing a common sequence of commands on a particular kind of value, often one you want to avoid acting on directly. One of the most common monads is the "maybe" or optional value monad, which wraps a value that could be either nothing or something. By using a monad instead of the raw value, you can protect your code from exposure to null values. Likewise, a "state" monad can be used in a parser to algorithmically consume an input string using a repeatable sequence of steps that preserves the current state of the input from operation to operation. Also, since a monad is, by definition, a special kind of functor that also returns a monad, they can be chained together to describe any sequence of operations. In functional languages with lazy evaluation, monads are used where sequence of evaluation is important, such as in I/O. Due to this sequencing utility, they are sometimes referred to as "programmable semicolons." +> A monad is a type that provides two functions, [chain](#chain) and [ap](#applicative-functor). Monads provide an interface for executing a common sequence of commands on a particular kind of value, often one you want to avoid acting on directly. One of the most common monads is the "maybe" or optional value monad, which wraps a value that could be either nothing or something. By using a monad instead of the raw value, you can protect your code from exposure to null values. Likewise, a "state" monad can be used in a parser to algorithmically consume an input string using a repeatable sequence of steps that preserves the current state of the input from operation to operation. Also, since a monad is, by definition, a special kind of functor that also returns a monad, they can be chained together to describe any sequence of operations. In functional languages with lazy evaluation, monads are used where sequence of evaluation is important, such as in I/O. Due to this sequencing utility, they are sometimes referred to as "programmable semicolons." The simplest monad is the Identity monad. It simply wraps a value. @@ -253,12 +264,28 @@ let Identity = v => ({ bind: transform => transform(v) }) --- +## Chain + +> A chain is a container type that implements a chain function. The chain function takes another function to run on the contained value and returns a value in the same container. The passed function must also return a value in the same container. This is also known as bind, or flatmap in other languages. + +```js +['cat,dog','fish,bird'].chain((a) => a.split(',')) // ['cat','dog','fish','bird'] +``` + +--- + ## Comonad --- ## Applicative Functor +> An applicative functor is a container type that can have functions put in it. A function often called `ap` is available on the type which applies a function in the container to a value in another container of the same type. + +```js +[(a)=> a + 1].ap([1]) // [2] +``` + --- From 29ed5185bb18667e937d8f7104837c55c8a6bdfc Mon Sep 17 00:00:00 2001 From: Jethro Larson Date: Mon, 18 Jan 2016 19:07:21 -0800 Subject: [PATCH 2/3] Fixed missing hr --- readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/readme.md b/readme.md index 686b5d9..9680745 100644 --- a/readme.md +++ b/readme.md @@ -175,6 +175,8 @@ lift(n => n * 2)([2,3,4]); // [4,6,8] lift((a, b) => a * b)([1, 2], [3]); // [3, 6] ``` +--- + ## Referential Transparency > An expression that can be replaced with its value without changing the From 81af06dc4b962b4e2bfbf5b1a447bea886166b16 Mon Sep 17 00:00:00 2001 From: Jethro Larson Date: Mon, 18 Jan 2016 19:13:08 -0800 Subject: [PATCH 3/3] Cleaned up some vagaries --- readme.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/readme.md b/readme.md index 9680745..1e599b2 100644 --- a/readme.md +++ b/readme.md @@ -168,15 +168,13 @@ Simplest functor in javascript is an `Array` ## Lift -> A function which puts a function into a container and applies that function to its next argument(s). Map is a lift over a one-argument function, but the same principal can be used to combine multiple functors. +> Lift takes a function and runs it on values in a container type. Map is a lift over a one-argument function, but the same principal can be used to combine multiple containers of the same type. ```js lift(n => n * 2)([2,3,4]); // [4,6,8] lift((a, b) => a * b)([1, 2], [3]); // [3, 6] ``` ---- - ## Referential Transparency > An expression that can be replaced with its value without changing the @@ -256,7 +254,7 @@ The identity value is empty array `[]` ## Monad -> A monad is a type that provides two functions, [chain](#chain) and [ap](#applicative-functor). Monads provide an interface for executing a common sequence of commands on a particular kind of value, often one you want to avoid acting on directly. One of the most common monads is the "maybe" or optional value monad, which wraps a value that could be either nothing or something. By using a monad instead of the raw value, you can protect your code from exposure to null values. Likewise, a "state" monad can be used in a parser to algorithmically consume an input string using a repeatable sequence of steps that preserves the current state of the input from operation to operation. Also, since a monad is, by definition, a special kind of functor that also returns a monad, they can be chained together to describe any sequence of operations. In functional languages with lazy evaluation, monads are used where sequence of evaluation is important, such as in I/O. Due to this sequencing utility, they are sometimes referred to as "programmable semicolons." +> A monad is a container type that provides two functions, [chain](#chain) and [ap](#applicative-functor). Monads provide an interface for executing a common sequence of commands on a particular kind of value, often one you want to avoid acting on directly. One of the most common monads is the "maybe" or optional value monad, which wraps a value that could be either nothing or something. By using a monad instead of the raw value, you can protect your code from exposure to null values. Likewise, a "state" monad can be used in a parser to algorithmically consume an input string using a repeatable sequence of steps that preserves the current state of the input from operation to operation. Also, since a monad is, by definition, a special kind of functor that also returns a monad, they can be chained together to describe any sequence of operations. In functional languages with lazy evaluation, monads are used where sequence of evaluation is important, such as in I/O. Due to this sequencing utility, they are sometimes referred to as "programmable semicolons." The simplest monad is the Identity monad. It simply wraps a value.