forked from evilsoft/crocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding in the
compose2
combinator (evilsoft#431)
* Adding in the `compose2` combinator * Updates per review + minor doc fixes
- Loading branch information
1 parent
ce1fbd8
commit b2f8a77
Showing
10 changed files
with
228 additions
and
24 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
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,16 @@ | ||
/** @license ISC License (c) copyright 2019 original and current authors */ | ||
/** @author Dale Francis (dalefrancis88) */ | ||
|
||
const curry = require('../core/curry') | ||
const isFunction = require('../core/isFunction') | ||
|
||
// compose2 :: (c -> d -> e) -> (a -> c) -> (b -> d) -> a -> b -> e | ||
function compose2(f, g, h, x, y) { | ||
if(!isFunction(f) || !isFunction(g) || !isFunction(h)) { | ||
throw new TypeError('compose2: First, second and third arguments must be functions') | ||
} | ||
|
||
return curry(f)(g(x), h(y)) | ||
} | ||
|
||
module.exports = curry(compose2) |
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,59 @@ | ||
const test = require('tape') | ||
const helpers = require('../test/helpers') | ||
|
||
const bindFunc = helpers.bindFunc | ||
|
||
const isFunction = require('../core/isFunction') | ||
|
||
const compose2 = require('./compose2') | ||
|
||
test('compose2', t => { | ||
const fn = bindFunc(compose2) | ||
const f = x => y => x * y | ||
const g = x => x - 1 | ||
const h = x => x + 1 | ||
const x = 22 | ||
const y = 1 | ||
|
||
t.ok(isFunction(compose2), 'is a function') | ||
|
||
const err = /^TypeError: compose2: First, second and third arguments must be functions/ | ||
t.throws(fn(undefined, g, h, x, y), err, 'throws with first arg undefined') | ||
t.throws(fn(null, g, h, x, y), err, 'throws with first arg null') | ||
t.throws(fn(0, g, h, x, y), err, 'throws with first arg falsey number') | ||
t.throws(fn(1, g, h, x, y), err, 'throws with first arg truthy number') | ||
t.throws(fn('', g, h, x, y), err, 'throws with first arg falsey string') | ||
t.throws(fn('string', g, h, x, y), err, 'throws with first arg truthy string') | ||
t.throws(fn(false, g, h, x, y), err, 'throws with first arg false') | ||
t.throws(fn(true, g, h, x, y), err, 'throws with first arg true') | ||
t.throws(fn({}, g, h, x, y), err, 'throws with first arg an object') | ||
t.throws(fn([], g, h, x, y), err, 'throws with first arg an array') | ||
|
||
t.throws(fn(f, undefined, h, x, y), err, 'throws with second arg undefined') | ||
t.throws(fn(f, null, h, x, y), err, 'throws with second arg null') | ||
t.throws(fn(f, 0, h, x, y), err, 'throws with second arg falsey number') | ||
t.throws(fn(f, 1, h, x, y), err, 'throws with second arg truthy number') | ||
t.throws(fn(f, '', h, x, y), err, 'throws with second arg falsey string') | ||
t.throws(fn(f, 'bling', h, x, y), err, 'throws with second arg truthy string') | ||
t.throws(fn(f, false, h, x, y), err, 'throws with second arg false') | ||
t.throws(fn(f, true, h, x, y), err, 'throws with second arg true') | ||
t.throws(fn(f, {}, h, x, y), err, 'throws with second arg an object') | ||
t.throws(fn(f, [], h, x, y), err, 'throws with second arg an array') | ||
|
||
t.throws(fn(f, g, undefined, x, y), err, 'throws with third arg undefined') | ||
t.throws(fn(f, g, null, x, y), err, 'throws with third arg null') | ||
t.throws(fn(f, g, 0, x, y), err, 'throws with third arg falsey number') | ||
t.throws(fn(f, g, 1, x, y), err, 'throws with third arg truthy number') | ||
t.throws(fn(f, g, '', x, y), err, 'throws with third arg falsey string') | ||
t.throws(fn(f, g, 'string', x, y), err, 'throws with third arg truthy string') | ||
t.throws(fn(f, g, false, x, y), err, 'throws with third arg false') | ||
t.throws(fn(f, g, true, x, y), err, 'throws with third arg true') | ||
t.throws(fn(f, g, {}, x, y), err, 'throws with third arg an object') | ||
t.throws(fn(f, g, [], x, y), err, 'throws with third arg an array') | ||
|
||
const result = fn(f, g, h, x, y) | ||
|
||
t.equal(result(), 42, 'returns expected result') | ||
|
||
t.end() | ||
}) |
Oops, something went wrong.