Merge pull request #22 from jethrolarson/master

Simplified some language and added a couple definitions
This commit is contained in:
hemanth.hm 2016-02-23 19:48:46 +05:30
commit 468cbc118c

View file

@ -153,18 +153,13 @@ Points-free function definitions look just like normal assignments without `func
## Categories ## Categories
--- > Objects with associated functions that adhere certain rules. E.g. [monoid](#monoid)
## Container Type
> A data structure that can have any other type, function, or data structure in it.
Array in JavaScript is a container type.
--- ---
## Functor ## Functor
> A container type that can be mapped over and returns the same kind of container. > An object with a `map` function. `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`
@ -173,9 +168,21 @@ Simplest functor in javascript is an `Array`
``` ```
--- ---
## Pointed Functor
> A functor with an `of` method. `Of` puts _any_ single value into a functor.
Array Implementation:
```js
Array.prototype.of = (v) => [v];
[].of(1) // [1]
```
---
## Lift ## Lift
> Lift takes a function with n arguments and returns one that can be run on n containers of the same type. > Lift is like `map` except it can be applied to multiple functors.
Map is the same as a lift over a one-argument function: Map is the same as a lift over a one-argument function:
@ -186,7 +193,6 @@ Unlike map lift can be used to combine values from multiple arrays:
``` ```
lift((a, b) => a * b)([1, 2], [3]); // [3, 6] lift((a, b) => a * b)([1, 2], [3]); // [3, 6]
``` ```
Lift can apply to any [Applicative Functor](#applicative-functor).
--- ---
@ -269,54 +275,29 @@ The identity value is empty array `[]`
## Monad ## Monad
> A monad is a container type that provides two functions, [chain](#chain) and `of`. 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 an object with [`of`](#pointed-functor) and `chain` functions. `Chain` is like [map](#functor) except it unnests the resulting nested object.
The simplest monad is the Identity monad. It simply wraps a value.
```js ```js
let Identity = v => ({ ['cat,dog','fish,bird'].chain(a => a.split(',')) // ['cat','dog','fish','bird']
val: v,
chain: transform => transform(this.val),
of: v => this.val
})
// Function that increments value and then wraps with Identity. //Contrast to map
let increment = v => Identity(v + 1) ['cat,dog','fish,bird'].map(a => a.split(',')) // [['cat','dog'], ['fish','bird']]
// Use chain to apply function to wrapped values
let incrementIdentity = id => id.chain(increment)
incrementIdentity(Identity(1)) // Identity(2)
//Contrast to using a map, where increment would cause nested Identities
id.map(increment) // Identity(Identity(2))
``` ```
---
## 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 ## Comonad
> A container type that has `extract` and `extend` functions. > An object that has `extract` and `extend` functions.
```js ```js
let CoIdentity = v => ({ let CoIdentity = v => ({
val: v, val: v,
extract: this.v, extract: this.v,
extend: f => f(this) extend: f => CoIdentity(f(this))
}) })
``` ```
Extract takes a value out of a container. Essentially it's the opposite of `of`. Extract takes a value out of a functor.
```js ```js
CoIdentity(1).extract() // 1 CoIdentity(1).extract() // 1
``` ```
@ -329,7 +310,7 @@ CoIdentity(1).extend(co => co.extract() + 1) // CoIdentity(2)
## Applicative Functor ## 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. > An applicative functor is an object with an `ap` function. `Ap` applies a function in the object to a value in another object of the same type.
```js ```js
[(a)=> a + 1].ap([1]) // [2] [(a)=> a + 1].ap([1]) // [2]
@ -339,14 +320,18 @@ CoIdentity(1).extend(co => co.extract() + 1) // CoIdentity(2)
## Morphism ## Morphism
> A transformation function.
--- ---
## Isomorphic ## Isomorphism
> Two objects are Isomorphic is they satisfy the condition: `compose(to, from) == identity` and `compose(from, to) == identity` > A pair of transformations between 2 types of objects that is structural in nature and no data is lost.
For example, 2D coordinates could be stored as an array `[2,3]` or object `{x: 2, y: 3}`.
```js ```js
const pairToCoords = (arr) => ({x: arr[0], y: arr[1]}) // Providing functions to convert in both directions makes them isomorphic.
const pairToCoords = (pair) => ({x: pair[0], y: pair[1]})
const coordsToPair = (coords) => [coords.x, coords.y] const coordsToPair = (coords) => [coords.x, coords.y]