Delint, fix TOC, minor tweaks

This commit is contained in:
jethro larson 2017-06-07 10:45:26 -07:00
parent a30176c55c
commit 1601ecd859

View file

@ -35,8 +35,6 @@ __Table of Contents__
* [Value](#value) * [Value](#value)
* [Constant](#constant) * [Constant](#constant)
* [Functor](#functor) * [Functor](#functor)
* [Preserves identity](#preserves-identity)
* [Composable](#composable)
* [Pointed Functor](#pointed-functor) * [Pointed Functor](#pointed-functor)
* [Lift](#lift) * [Lift](#lift)
* [Referential Transparency](#referential-transparency) * [Referential Transparency](#referential-transparency)
@ -103,9 +101,9 @@ Partially applying a function means creating a new function by pre-filling some
// Helper to create partially applied functions // Helper to create partially applied functions
// Takes a function and some arguments // Takes a function and some arguments
const partial = (f, ...args) => const partial = (f, ...args) =>
// returns a function that takes the rest of the arguments // returns a function that takes the rest of the arguments
(...moreArgs) => (...moreArgs) =>
// and calls the original function with all of them // and calls the original function with all of them
f(...args, ...moreArgs) f(...args, ...moreArgs)
// Something to apply // Something to apply
@ -154,9 +152,9 @@ ie. they allow referencing a scope after the block in which the variables were d
```js ```js
const addTo = x => y => x + y; const addTo = x => y => x + y
var addToFive = addTo(5); var addToFive = addTo(5)
addToFive(3); //returns 8 addToFive(3) // returns 8
``` ```
The function ```addTo()``` returns a function(internally called ```add()```), lets store it in a variable called ```addToFive``` with a curried call having parameter 5. The function ```addTo()``` returns a function(internally called ```add()```), lets store it in a variable called ```addToFive``` with a curried call having parameter 5.
@ -173,12 +171,12 @@ A closure is a function that encloses its surrounding state by referencing field
__Further reading/Sources__ __Further reading/Sources__
* [Lambda Vs Closure](http://stackoverflow.com/questions/220658/what-is-the-difference-between-a-closure-and-a-lambda) * [Lambda Vs Closure](http://stackoverflow.com/questions/220658/what-is-the-difference-between-a-closure-and-a-lambda)
* [JavaScript Closures highly voted disucussion](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work) * [How do JavaScript Closures Work?](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work)
## Auto Currying ## Auto Currying
Transforming a function that takes multiple arguments into one that if given less than its correct number of arguments returns a function that takes the rest. When the function gets the correct number of arguments it is then evaluated. Transforming a function that takes multiple arguments into one that if given less than its correct number of arguments returns a function that takes the rest. When the function gets the correct number of arguments it is then evaluated.
lodash & ramda have a `curry` function that works this way. lodash & Ramda have a `curry` function that works this way.
```js ```js
const add = (x, y) => x + y const add = (x, y) => x + y
@ -295,7 +293,7 @@ Math.abs(Math.abs(10))
``` ```
```js ```js
sort(sort(sort([2,1]))) sort(sort(sort([2, 1])))
``` ```
## Point-Free Style ## Point-Free Style
@ -406,12 +404,12 @@ john.age + five === ({name: 'John', age: 30}).age + (5)
An object that implements a `map` function which, while running over each value in the object to produce a new object, adheres to two rules: An object that implements a `map` function which, while running over each value in the object to produce a new object, adheres to two rules:
### Preserves identity __Preserves identity__
``` ```
object.map(x => x) ≍ object object.map(x => x) ≍ object
``` ```
### Composable __Composable__
``` ```
object.map(compose(f, g)) ≍ object.map(g).map(f) object.map(compose(f, g)) ≍ object.map(g).map(f)
@ -521,9 +519,9 @@ Lazy evaluation is a call-by-need evaluation mechanism that delays the evaluatio
```js ```js
const rand = function*() { const rand = function*() {
while (1 < 2) { while (1 < 2) {
yield Math.random() yield Math.random()
} }
} }
``` ```
@ -585,7 +583,7 @@ A monad is an object with [`of`](#pointed-functor) and `chain` functions. `chain
```js ```js
// Implementation // Implementation
Array.prototype.chain = function(f){ Array.prototype.chain = function (f) {
return this.reduce((acc, it) => acc.concat(f(it)), []) return this.reduce((acc, it) => acc.concat(f(it)), [])
} }
@ -605,7 +603,7 @@ An object that has `extract` and `extend` functions.
```js ```js
const CoIdentity = (v) => ({ const CoIdentity = (v) => ({
val: v, val: v,
extract () { extract () {
return this.val return this.val
}, },
@ -633,7 +631,7 @@ An applicative functor is an object with an `ap` function. `ap` applies a functi
```js ```js
// Implementation // Implementation
Array.prototype.ap = function(xs){ Array.prototype.ap = function (xs) {
return this.reduce((acc, f) => acc.concat(xs.map(f)), []) return this.reduce((acc, f) => acc.concat(xs.map(f)), [])
} }
@ -703,16 +701,16 @@ Make array a setoid:
```js ```js
Array.prototype.equals = function (arr) { Array.prototype.equals = function (arr) {
const len = this.length const len = this.length
if (len !== arr.length) { if (len !== arr.length) {
return false return false
}
for (let i = 0; i < len; i++) {
if (this[i] !== arr[i]) {
return false
} }
for (let i = 0; i < len; i++) { }
if (this[i] !== arr[i]) { return true
return false
}
}
return true
} }
;[1, 2].equals([1, 2]) // true ;[1, 2].equals([1, 2]) // true
@ -784,8 +782,8 @@ A Sum type is the combination of two types together into another one. It is call
JavaScript doesn't have types like this but we can use `Set`s to pretend: JavaScript doesn't have types like this but we can use `Set`s to pretend:
```js ```js
// imagine that rather than sets here we have types that can only have these values // imagine that rather than sets here we have types that can only have these values
const bools = new Set([true, false]); const bools = new Set([true, false])
const halfTrue = new Set(['half-true']); const halfTrue = new Set(['half-true'])
// The weakLogic type contains the sum of the values from bools and halfTrue // The weakLogic type contains the sum of the values from bools and halfTrue
const weakLogicValues = new Set([...bools, ...halfTrue]) const weakLogicValues = new Set([...bools, ...halfTrue])
@ -818,22 +816,22 @@ Option is useful for composing functions that might not return a value.
// Naive definition // Naive definition
const Some = (v) => ({ const Some = (v) => ({
val: v, val: v,
map(f) { map (f) {
return Some(f(this.val)) return Some(f(this.val))
}, },
chain(f) { chain (f) {
return f(this.val) return f(this.val)
} }
}) })
const None = () => ({ const None = () => ({
map(f){ map (f) {
return this return this
}, },
chain(f){ chain (f) {
return this return this
} }
}) })
// maybeProp :: (String, {a}) -> Option a // maybeProp :: (String, {a}) -> Option a