Skip to content

Instantly share code, notes, and snippets.

@oleschoenburg
Created March 30, 2013 21:58
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 oleschoenburg/5278541 to your computer and use it in GitHub Desktop.
Save oleschoenburg/5278541 to your computer and use it in GitHub Desktop.
List out of Lambda, code from @stevelosh
// List
var empty_list = function(selector) {
return selector(undefined, undefined, true);
}
var is_empty = function(list){
return list(function (h, t, e) {
return e;
});
}
var prepend = function(el, list) {
return function(selector) {
return selector(el, list, false);
}
}
var head = function(list){
return list(function(h, t, e){
return h;
})
}
var tail = function(list){
return list(function(h, t, e){
return t;
})
}
// Helpers
var not = function(x) {
if (x) {
return false;
} else {
return true;
}
}
var and = function(a, b){
if (a){
if (b){
return true;
} else {
return false;
}
} else {
return false;
}
}
var or = function(a, b){
if (a) {
return true;
} else {
if (b) {
return true;
} else {
return false;
}
}
}
//High-order functions
var map = function(fn, l){
if (is_empty(l)){
return empty_list;
} else {
return prepend(fn(head(l)), map(fn, tail(l)));
}
}
var filter = function(fn, l){
if(is_empty(l)){
empty_list;
} else if (fn(head(l))) {
return (prepend(head(l), filter(fn, tail(l))))
} else {
return filter(fn, tail(l));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment