Created
October 22, 2012 15:25
-
-
Save alescode/3932060 to your computer and use it in GitHub Desktop.
JS functional functions by github.com/targen
This file contains hidden or 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
function id(x) { | |
return x; | |
} | |
function const_(x) { | |
return function(_) { | |
return x; | |
}; | |
} | |
function compose(f) { | |
return function(g) { | |
return function(x) { | |
return f (g (x)); | |
}; | |
}; | |
} | |
function flip(f) { | |
return function(x) { | |
return function (y) { | |
return f (y) (x); | |
}; | |
}; | |
} | |
function join(f) { | |
return function(x) { | |
return f (x) (x); | |
}; | |
} | |
function liftM2(f) { | |
return function (g) { | |
return function(h) { | |
return function(x) { | |
return f (g (x)) (h (x)); | |
}; | |
}; | |
}; | |
} | |
function add(x) { | |
return function(y) { | |
return x + y; | |
}; | |
} | |
function multiply(x) { | |
return function(y) { | |
return x * y; | |
}; | |
} | |
function div(x) { | |
return function(y) { | |
return x / y; | |
}; | |
} | |
function pair(a) { | |
return function(b) { | |
return [a, b]; | |
}; | |
} | |
function fst(p) { | |
return p[0]; | |
} | |
function snd(p) { | |
return p[1]; | |
} | |
var split = function(x) { | |
return pair (x) (x); | |
} | |
var first = function(f) { | |
return function(p) { | |
return pair (fst (p)) (f (snd (p))); | |
}; | |
} | |
var second = function(f) { | |
return function(p) { | |
return pair (f (fst (p))) (snd (p)); | |
}; | |
} | |
var starstarstar = function(f) { | |
return function(g) { | |
return compose (first (f)) (second (g)); | |
}; | |
} | |
var andandand = function(f) { | |
return function(g) { | |
return compose (starstarstar (f) (g)) (split); | |
}; | |
} | |
function cons(x) { | |
return function(xs) { | |
return [x].concat(xs); | |
}; | |
} | |
function empty(xs) { | |
return xs.length == 0; | |
} | |
function head(xs) { | |
return xs[0]; | |
} | |
function tail(xs) { | |
return xs.slice(1); | |
} | |
function foldr(f) { | |
return function(z) { | |
return function(xs) { | |
return empty (xs) ? z : f (head (xs)) (foldr (f) (z) (tail (xs))); | |
}; | |
}; | |
} | |
function foldl(f) { | |
return function(z) { | |
return function(xs) { | |
return empty (xs) ? z : foldl (f) (f (z) (head (xs))) (tail (xs)); | |
}; | |
}; | |
} | |
function map(f) { | |
return foldr (compose (cons) (f)) ([]); | |
} | |
function unfoldr(f) { | |
return function(b) { | |
var p = f (b); | |
return empty (p) ? p : cons (head (p)) (unfoldr (f) (head (tail (p)))); | |
} | |
} | |
var enumFromTo = flip (function(y) { | |
return unfoldr (function(b) { return b > y ? [] : [b, b + 1]; }); | |
} | |
); | |
function zip(xs) { | |
return function(ys) { | |
return empty (xs) ? [] : empty (ys) ? [] : cons (pair (head (xs)) (head (ys))) (zip (tail (xs)) (tail (ys))); | |
}; | |
} | |
var unzip = foldr (liftM2 (starstarstar) (compose (cons) (fst)) (compose (cons) (snd))) (pair ([]) ([])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment