Created
December 14, 2017 16:46
-
-
Save bellentuck/2d8bf9365289264e3ec98074b405e40e to your computer and use it in GitHub Desktop.
JS predicate combinator boilerplate. Riffing off Jack Firth's "Predicates in Javascript"
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
// Firth: https://codepen.io/Universalist/post/predicates-in-javascript | |
(function(predicateCombinators) { | |
// CPL predication | |
function and(p1, p2) { | |
return function(x) { | |
return p1(x) && p2(x); | |
} | |
} | |
function or(p1, p2) { | |
return function(x) { | |
return p1(x) || p2(x); | |
} | |
} | |
function not(p) { | |
return function(x) { | |
return !p(x); | |
} | |
} | |
function nor(p1, p2) { | |
return function(x) { | |
return !p1(x) && !p2(x); | |
} | |
} | |
function nand(p1, p2) { | |
return function(x) { | |
return !(p1(x) && p2(x)) && (p1(x) || p2(x)); | |
} | |
} | |
// Positive and negative | |
function positive(x) { | |
return x > 0; | |
} | |
function negative(x) { | |
return x < 0; | |
} | |
var nonzero = or(negative, positive); | |
// Inequalities | |
function greater(x) { | |
return function(y) { | |
return y > x; | |
} | |
} | |
function less(x) { | |
return function(y) { | |
return y < x; | |
} | |
} | |
function equal(x) { | |
return function(y) { | |
return y === x; | |
} | |
} | |
// Kinds of numbers | |
function natural(x) { | |
return x >= 1; | |
} | |
function whole(x) { | |
return x >= 0; | |
} | |
function int(x) { | |
return x === Math.round(x); | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment