Last active
August 29, 2015 14:06
-
-
Save L8D/3a7e0c449313643f447f to your computer and use it in GitHub Desktop.
things
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 Tuple(a, b, c, d, e, f, g) { | |
function tuplable(transformer) { | |
return transformer(a, b, c, d, e, f, g); | |
}; | |
tuplable.toString = function() { | |
return 'Tuple (' + [a, b, c, d, e, f, g].filter(function(item) { | |
return item != null; | |
}).join(', ') + ')'; | |
}; | |
return tuplable; | |
} | |
var EmptyList = { | |
toString: function() { | |
return 'List []'; | |
} | |
}; | |
function List(head, rest) { | |
function listable(iterator) { | |
return iterator(head, rest); | |
}; | |
listable.toString = function() { | |
return 'List [' + toJS(this).join(', ') + ']'; | |
}; | |
return listable; | |
} | |
function map(iterator, list) { | |
if (list === EmptyList) { | |
return list; | |
} else { | |
return list(function(head, rest) { | |
return List(iterator(head), map(iterator, rest)); | |
}); | |
} | |
} | |
function filter(qualifier, list) { | |
if (list === EmptyList) { | |
return list; | |
} else { | |
return list(function(head, rest) { | |
if (qualifier(head)) { | |
return List(head, filter(qualifier, rest)); | |
} else { | |
return filter(qualifier, rest); | |
} | |
}); | |
} | |
} | |
function reduce(accumulator, value, list) { | |
if (list === EmptyList) { | |
return value; | |
} else { | |
return list(function(head, rest) { | |
return reduce(accumulator, accumulator(value, head), rest); | |
}); | |
} | |
} | |
function concat(list, list$) { | |
if (list === EmptyList) { | |
return list$; | |
} else { | |
return list(function(head, rest) { | |
return List(head, concat(rest, list$)); | |
}); | |
} | |
} | |
function concatl(list) { | |
return reduce(function(result, list) { | |
return concat(result, list); | |
}, EmptyList, list); | |
} | |
function flatmap(iterator, list) { | |
if (list === EmptyList) { | |
return list; | |
} else { | |
return list(function(head, rest) { | |
return concat(iterator(head), flatmap(iterator, rest)); | |
}); | |
} | |
} | |
function fromJS(array) { | |
return array.reduceRight(function(list, element) { | |
return List(element, list); | |
}, EmptyList); | |
} | |
function toJS(list) { | |
return reduce(function(array, element) { | |
return array.concat([element]); | |
}, [], list); | |
}; | |
var Nothing = { | |
toString: function() { | |
return 'Nothing'; | |
} | |
}; | |
function Just(value) { | |
function justable(iterator) { | |
return iterator(value); | |
} | |
justable.toString = function() { | |
return 'Just ' + this; | |
}; | |
return justable; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment