-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add converge (Phoenix) to combinators. (#333)
* Add `converge` (Phoenix) to combinators. * Update combinator names for `converge` * Update `converge` test example usage * Get famous * Move `converge` to the correct `combinators` directory and expose * Add `combinators/converge` to the appropriate docs pages * Add pirates to `converge` docs * Add `converge` to `combinators` search terms * Update helper paths, error message, license date, error checks * Remove dep destructuring and add signatures to `converge` example code * Give `converge` example some room to breathe * Reflect unknown type of `Maybe` in example fn results
- Loading branch information
Showing
7 changed files
with
160 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** @license ISC License (c) copyright 2018 original and current authors */ | ||
/** @author Matt Ross (amsross) */ | ||
|
||
const curry = require('../core/curry') | ||
const isFunction = require('../core/isFunction') | ||
|
||
// converge (Phoenix or Starling Prime) | ||
// (b -> c -> d) -> (a -> b) -> (a -> c) -> a -> d | ||
function converge(f, g, h, x) { | ||
if(!isFunction(f) || !isFunction(g) || !isFunction(h)) { | ||
throw new TypeError('converge: Functions required for first three arguments') | ||
} | ||
|
||
return curry(f)(g(x), h(x)) | ||
} | ||
|
||
module.exports = curry(converge) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const test = require('tape') | ||
const helpers = require('../test/helpers') | ||
|
||
const bindFunc = helpers.bindFunc | ||
const isFunction = require('../core/isFunction') | ||
|
||
const sub = require('./converge') | ||
|
||
test('converge (Big Phi or S\' combinator)', t => { | ||
const s = bindFunc(sub) | ||
const x = 67 | ||
const f = x => y => x + y | ||
const g = x => x - 1 | ||
const h = x => x + 1 | ||
|
||
t.ok(isFunction(sub), 'is a function') | ||
|
||
const err = /converge: Functions required for first three arguments/ | ||
t.throws(s(undefined, g, h, x), err, 'throws with first arg undefined') | ||
t.throws(s(null, g, h, x), err, 'throws with first arg null') | ||
t.throws(s(0, g, h, x), err, 'throws with first arg falsey number') | ||
t.throws(s(1, g, h, x), err, 'throws with first arg truthy number') | ||
t.throws(s('', g, h, x), err, 'throws with first arg falsey string') | ||
t.throws(s('string', g, h, x), err, 'throws with first arg truthy string') | ||
t.throws(s(false, g, h, x), err, 'throws with first arg false') | ||
t.throws(s(true, g, h, x), err, 'throws with first arg true') | ||
t.throws(s({}, g, h, x), err, 'throws with first arg an object') | ||
t.throws(s([], g, h, x), err, 'throws with first arg an array') | ||
|
||
t.throws(s(f, undefined, h, x), err, 'throws with second arg undefined') | ||
t.throws(s(f, null, h, x), err, 'throws with second arg null') | ||
t.throws(s(f, 0, h, x), err, 'throws with second arg falsey number') | ||
t.throws(s(f, 1, h, x), err, 'throws with second arg truthy number') | ||
t.throws(s(f, '', h, x), err, 'throws with second arg falsey string') | ||
t.throws(s(f, 'bling', h, x), err, 'throws with second arg truthy string') | ||
t.throws(s(f, false, h, x), err, 'throws with second arg false') | ||
t.throws(s(f, true, h, x), err, 'throws with second arg true') | ||
t.throws(s(f, {}, h, x), err, 'throws with second arg an object') | ||
t.throws(s(f, [], h, x), err, 'throws with second arg an array') | ||
|
||
t.throws(s(f, g, undefined, x), err, 'throws with third arg undefined') | ||
t.throws(s(f, g, null, x), err, 'throws with third arg null') | ||
t.throws(s(f, g, 0, x), err, 'throws with third arg falsey number') | ||
t.throws(s(f, g, 1, x), err, 'throws with third arg truthy number') | ||
t.throws(s(f, g, '', x), err, 'throws with third arg falsey string') | ||
t.throws(s(f, g, 'string', x), err, 'throws with third arg truthy string') | ||
t.throws(s(f, g, false, x), err, 'throws with third arg false') | ||
t.throws(s(f, g, true, x), err, 'throws with third arg true') | ||
t.throws(s(f, g, {}, x), err, 'throws with third arg an object') | ||
t.throws(s(f, g, [], x), err, 'throws with third arg an array') | ||
|
||
const result = sub(f, g, h, x) | ||
|
||
t.equal(result, 134, 'returns expected result') | ||
|
||
t.end() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters