Skip to content

Instantly share code, notes, and snippets.

@bellentuck
Created December 14, 2017 16:46
Show Gist options
  • Save bellentuck/2d8bf9365289264e3ec98074b405e40e to your computer and use it in GitHub Desktop.
Save bellentuck/2d8bf9365289264e3ec98074b405e40e to your computer and use it in GitHub Desktop.
JS predicate combinator boilerplate. Riffing off Jack Firth's "Predicates in Javascript"
// 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