Last active
August 29, 2015 14:13
-
-
Save reidev275/119dbf50293a8a384cd3 to your computer and use it in GitHub Desktop.
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
var Ar = { | |
filter: function(fn, list) { | |
if (!list[0]) return []; | |
if (fn(list[0])) return [list[0]].concat(Ar.filter(fn, list.slice(1))); | |
return [].concat(Ar.filter(fn, list.slice(1))); | |
}, | |
first: function(fn, list) { | |
if (!list[0]) return null; | |
if (fn(list[0])) return list[0]; | |
return Ar.first(fn, list.slice(1)); | |
}, | |
map: function (fn, list) { | |
if (!list[0]) return []; | |
return [fn(list[0])].concat(Ar.map(fn, list.slice(1))); | |
} | |
}; | |
var numbers = [ 1,2,3 ]; | |
function isEven(x) { | |
return x % 2 === 0; | |
} | |
document.write('<br>filter: ' + Ar.filter(isEven, numbers)); | |
function square(x) { | |
return x * x; | |
} | |
document.write('<br>map^2: ' + Ar.map(square, numbers)); | |
function greaterThan2(x) { | |
return x > 2; | |
} | |
document.write('<br>first: ' + Ar.first(greaterThan2, numbers)); |
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
function Partial(fn /*, args...*/) { | |
var slice = Array.prototype.slice; | |
var args = slice.call(arguments, 1); | |
return function() { | |
return fn.apply(this, args.concat(slice.call(arguments, 0))); | |
}; | |
} | |
var squareList = Partial(Ar.map, square); | |
document.write('<br>map^2: ' + squareList(numbers)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment