Created
September 18, 2013 15:29
-
-
Save rtt/6610903 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
set: function (collection) { | |
// returns a set (as a list...) from a collection | |
// in other words, uniques | |
var o = {}, r = [], l = collection.length, i = 0; | |
for (; i < l; i++) { | |
if (!(collection[i] in o)) { | |
o[collection[i]] = true; | |
r.push(collection[i]); | |
} | |
} | |
return r; | |
}, | |
map: function (collection, lambda) { | |
// maps a collection by a lambda | |
var r = [], i = 0; l = collection.length; | |
for (; i < l; i++) { | |
r.push(lambda(collection[i])); | |
} | |
return r; | |
}, | |
filter: function (collection, predicate) { | |
// filters a collection according to a predicate | |
var r = [], i = 0, l = collection.length; | |
for (; i < l; i++) { | |
if (predicate(collection[i])) { | |
r.push(collection[i]); | |
} | |
} | |
return r; | |
}, | |
all: function (collection) { | |
// equvalent of python's all() | |
return this.filter(collection, function (el) { return !!el; }); | |
}, | |
nall: function (collection) { | |
// reverse of all() | |
return this.filter(collection, function (el) { return !!!el; }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment