Skip to content

Instantly share code, notes, and snippets.

@rtt
Created September 18, 2013 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtt/6610903 to your computer and use it in GitHub Desktop.
Save rtt/6610903 to your computer and use it in GitHub Desktop.
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