-
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.
Merge pull request #16 from evilsoft/pred-crock
Add Pred crock
- Loading branch information
Showing
8 changed files
with
333 additions
and
71 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,61 @@ | ||
/** @license ISC License (c) copyright 2016 original and current authors */ | ||
/** @author Ian Hofmann-Hicks (evil) */ | ||
|
||
const isFunction = require('../internal/isFunction') | ||
const isType = require('../internal/isType') | ||
|
||
const _inspect = require('../funcs/inspect') | ||
|
||
const constant = require('../combinators/constant') | ||
const composeB = require('../combinators/composeB') | ||
|
||
const _type = | ||
constant('Pred') | ||
|
||
const _empty = | ||
() => Pred(constant(true)) | ||
|
||
function Pred(runWith) { | ||
if(!isFunction(runWith)) { | ||
throw new TypeError('Pred: Predicate function required') | ||
} | ||
|
||
const type = | ||
_type | ||
|
||
const inspect = | ||
constant(`Pred${_inspect(runWith)}`) | ||
|
||
const empty = | ||
_empty | ||
|
||
const value = | ||
constant(runWith) | ||
|
||
function concat(m) { | ||
if(!(m && isType(type(), m))) { | ||
throw new TypeError('Pred.concat: Pred required') | ||
} | ||
|
||
return Pred(x => runWith(x) && m.runWith(x)) | ||
} | ||
|
||
function contramap(fn) { | ||
if(!isFunction(fn)) { | ||
throw new TypeError('Pred.contramap: Function required') | ||
} | ||
|
||
return Pred(composeB(runWith, fn)) | ||
} | ||
|
||
return { | ||
runWith, inspect, type, | ||
value, empty, concat, | ||
contramap | ||
} | ||
} | ||
|
||
Pred.empty = _empty | ||
Pred.type = _type | ||
|
||
module.exports = Pred |
Oops, something went wrong.