Skip to content

Commit

Permalink
Merge pull request #3 from earldouglas/snapshots/0.6
Browse files Browse the repository at this point in the history
Release 0.6.0
  • Loading branch information
earldouglas committed Jan 9, 2015
2 parents daadb37 + fe4bdc5 commit b2df0d0
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 291 deletions.
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,19 @@ var d = c.map(function (x) { return x + 1; }); // some(42)

## Documentation

### compose(f, g)
### array.contains(xs, x)

`contains` takes an array and an element, and returns whether the element
exists at least once in the array.

*Example:*

```javascript
var yep = contains([1,2,3], 2); // true
var nope = contains([1,2,3], 4); // false
```

### fn.compose(f, g)

[*demo*](http://jsfiddle.net/earldouglas/8q1znL7n/)

Expand Down Expand Up @@ -55,7 +67,7 @@ var five = compose(inc, square)(2); // inc(square(2)) == (2 ^ 2) + 1

**g**: `function`, a unary function

### curry(f, args)
### fn.curry(f, args)

`curry` takes an n-ary function `f` and an optional array of arguments, and
returns a curried version of `f`, comprised of *n* nested unary functions
Expand Down Expand Up @@ -88,7 +100,7 @@ var fortyTwo = curry(mathemagic)(2)(20)(1);

**args**: `array`, [optional] arguments to apply to `f`

### memoize(f, cache)
### fn.memoize(f, cache)

`memoize` takes a function and an optional cache implementation, and
memoizes the function by backing it with the cache, if supplied, or a simple
Expand All @@ -111,7 +123,7 @@ var fastResult = cheapFn(42); // cheap cache lookup the second time

**cache**: `object`, [optional] a cache object with get(k) and put(k,v) functions

### lazy(f, cache)
### fn.lazy(f, cache)

`lazy` takes a function and an optional cache implementation, and creates a
function that, given input arguments, returns a lazy evaluator that will
Expand Down Expand Up @@ -176,7 +188,7 @@ An option instance exposes the following fields:

**value**: `any`, [optional] the value to wrap in an option

### collect(promises, callback)
### promise.collect(promises, callback)

Given an array of promises and a callback, passes the result of each promise
(in order) as an argument to the callback, and returns a single promise that
Expand Down Expand Up @@ -205,7 +217,7 @@ p is congruent to `Promise.resolve(2 * (20 + 1))`, or `Promise.resolve(42)`

**callback**: `function`, a function that takes as arguments the results of the promises

### valid(value)
### validation.valid(value)

`valid` constructs a "validation" representing a valid value.

Expand All @@ -222,7 +234,7 @@ A validation created by `valid` exposes the following fields:

**value**: `any`, a valid value to wrap in a validation

### invalid(errors)
### validation.invalid(errors)

`invalid` constructs a "validation" representing an invalid value, and
containing an array of errors.
Expand Down
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### 0.6.0

* Organized namespacing
* Array searching via `array.contains(xs, x)`
* 100% code coverage

### 0.5.0

* Encapsulate `cons()` and `nil` behind `list()`
Expand Down
50 changes: 0 additions & 50 deletions doc.sh

This file was deleted.

12 changes: 12 additions & 0 deletions lib/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

function contains(xs, x) {
for (var i = 0; i < xs.length; i++) {
if (xs[i] === x) {
return true;
}
}
return false;
}

module.exports.contains = contains;
100 changes: 0 additions & 100 deletions lib/function.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,11 @@
'use strict';

/**
*
* [*demo*](http://jsfiddle.net/earldouglas/8q1znL7n/)
*
* `compose` takes two unary functions `f` and `g`, and combines them into a
* single unary function `f ∘ g` that applies `g` to an input, passes the
* output to `f`, applies `f` to it, and returns the result.
*
* The application of `(f ∘ g)(x)` is equivalent to `f(g(x))`.
*
* *Example:*
*
* ```javascript
* function inc(x) {
* return x + 1;
* }
*
* function square(x) {
* return x * x;
* }
*
* var nine = compose(square, inc)(2); // square(inc(2)) == (2 + 1) ^ 2
* var five = compose(inc, square)(2); // inc(square(2)) == (2 ^ 2) + 1
* ```
*
* @param f {function} a unary function
* @param g {function} a unary function
*
*/
function compose(f, g) {
return function (x) {
return f(g(x));
};
}

/**
* `curry` takes an n-ary function `f` and an optional array of arguments, and
* returns a curried version of `f`, comprised of *n* nested unary functions
* where the arity of `f` is *n*.
*
* *Example:*
*
* ```javascript
* function add(x, y) {
* return x + y;
* };
*
* var add2 = curry(add)(2);
* var five = add2(3);
* ```
*
* *Example:*
*
* ```javascript
* function mathemagic(x, y, z) {
* return x * (y + z);
* };
*
* var fortyTwo = curry(mathemagic)(2)(20)(1);
* ```
*
* @param f {function} an n-ary function
* @param args {array} [optional] arguments to apply to `f`
*
*/
function curry(f, args) {
if (!args || !args.length) {
args = [];
Expand All @@ -87,26 +28,6 @@ function dumbCache() {
};
}

/**
* `memoize` takes a function and an optional cache implementation, and
* memoizes the function by backing it with the cache, if supplied, or a simple
* object-based cache otherwise.
*
* *Example:*
*
* ```javascript
* function expensiveFn(n) { ... }
*
* var cheapFn = memoize(expensiveFn);
*
* var slowResult = cheapFn(42); // expensive computation the first time
* var fastResult = cheapFn(42); // cheap cache lookup the second time
* ```
*
* @param f {function} an n-ary function
* @param cache {object} [optional] a cache object with get(k) and put(k,v) functions
*
*/
function memoize(f, cache) {
if (!cache) {
cache = dumbCache();
Expand All @@ -124,27 +45,6 @@ function memoize(f, cache) {
};
}

/**
* `lazy` takes a function and an optional cache implementation, and creates a
* function that, given input arguments, returns a lazy evaluator that will
* apply the function to the arguments only when needed, and only once if
* needed many times.
*
* *Example:*
*
* ```javascript
* function expensiveFn(n) { ... }
*
* var lazyVal = lazy(expensiveFn)(42); // lazily apply 42 -- no computation yet
*
* var slowResult = lazyVal.get(); // expensive computation the first time
* var fastResult = lazyVal.get(); // cheap cache lookup the second time
* ```
*
* @param f {function} an n-ary function
* @param cache {object} [optional] a cache object with get(k) and put(k,v) functions
*
*/
function lazy(f, cache) {
var fMemo = memoize(f, cache);
return function() {
Expand Down
22 changes: 1 addition & 21 deletions lib/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,6 @@ function _cons(head, tail) {
};
}

/**
* `list` constructs a linked list from a value, `head`, representing the first
* element in the list, and another list, `tail`, representing the rest of the
* constructed list. If `head` is null, `tail` is returned, and if `tail` is
* null, the empty list is returned.
*
* A list instance exposes the following fields:
*
* * `head` - the head of this list, if it is not empty
* * `tail` - the tail of this list, if it is not empty
* * `length` - returns the length of this list
* * `map(f)` - returns a new list created by applying `f` over this list
* * `flatMap(f)` - returns a new list created by applying `f` over this list and concatenating the results
* * `concat(l)` - returns the concatenation of this list with l
* * `toString()`
*
* @param head {any} the first element in the list
* @param tail {list} the rest of the list
*
*/
function list(head, tail) {
if (tail == null) {
return list(head, _nil);
Expand All @@ -50,4 +30,4 @@ function list(head, tail) {
}
}

exports.list = list;
module.exports = list;
18 changes: 1 addition & 17 deletions lib/option.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
'use strict';

/**
* `option` constructs a representation of an optional value, represented as
* either "some value" or "no value", depending on whether a non-null argument
* was supplied.
*
* An option instance exposes the following fields:
*
* * `empty`
* * `map(f)` - returns a new option by applying `f` over this option's value, and wrapping the result in an option
* * `flatMap(f)` - returns a new option by applying `f` over this option's value, and returning the result
* * `ap(a)` - assumes this option wraps a function, and returns a new option by mapping this option's function over the option `a` and returning the result
* * `toString()`
*
* @param value {any} [optional] the value to wrap in an option
*
*/
function option(value) {
if (value != null) {
return {
Expand All @@ -36,4 +20,4 @@ function option(value) {
}
}

exports.option = option;
module.exports = option;
26 changes: 0 additions & 26 deletions lib/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,6 @@

var curry = require('./function').curry;

/**
* Given an array of promises and a callback, passes the result of each promise
* (in order) as an argument to the callback, and returns a single promise that
* yields the result of the callback.
*
* Any of the promises can be 'lazy', implemented as a nullary function that
* returns a promise, and will be retrieved as late as possible.
*
* *Example:*
*
* ```javascript
* var p = collect([
* Promise.resolve(2),
* Promise.resolve(20),
* Promise.resolve(1)
* ], function (x, y, z) {
* return x * (y + z);
* });
* ```
*
* p is congruent to `Promise.resolve(2 * (20 + 1))`, or `Promise.resolve(42)`
*
* @param promises {array} an array of promises
* @param callback {function} a function that takes as arguments the results of the promises
*
*/
function collect(promises, callback) {
var f = curry(callback);
var p = promises.reduce(function (p1, p2) {
Expand Down
Loading

0 comments on commit b2df0d0

Please sign in to comment.