-
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.
better compose and pipe functions (#126)
- Loading branch information
Showing
8 changed files
with
179 additions
and
122 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,41 @@ | ||
/** @license ISC License (c) copyright 2016 original and current authors */ | ||
/** @author Ian Hofmann-Hicks (evil) */ | ||
|
||
const pipe = require('./pipe') | ||
const argsArray = require('../internal/argsArray') | ||
|
||
const identity = require('../combinators/identity') | ||
const isFunction = require('../predicates/isFunction') | ||
|
||
const err = 'compose: Functions required' | ||
|
||
function applyPipe(f, g) { | ||
if(!isFunction(g)) { | ||
throw new TypeError(err) | ||
} | ||
|
||
return function() { | ||
return g.call(null, f.apply(null, argsArray(arguments))) | ||
} | ||
} | ||
// compose : ((y -> z), (x -> y), ..., (a -> b)) -> a -> z | ||
function compose() { | ||
if(!arguments.length) { | ||
throw new TypeError('compose: At least one function required') | ||
throw new TypeError(err) | ||
} | ||
|
||
const fns = | ||
argsArray(arguments) | ||
argsArray(arguments).slice().reverse() | ||
|
||
if(fns.filter(x => !isFunction(x)).length) { | ||
throw new TypeError('compose: Only accepts functions') | ||
const head = | ||
fns[0] | ||
|
||
if(!isFunction(head)) { | ||
throw new TypeError(err) | ||
} | ||
|
||
return pipe.apply(null, fns.slice().reverse()) | ||
const tail = | ||
fns.slice(1).concat(identity) | ||
|
||
return tail.reduce(applyPipe, head) | ||
} | ||
|
||
module.exports = compose |
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
Oops, something went wrong.