Skip to content

Instantly share code, notes, and snippets.

@raganwald
Last active December 18, 2015 14:59
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 raganwald/5800937 to your computer and use it in GitHub Desktop.
Save raganwald/5800937 to your computer and use it in GitHub Desktop.
I may not be able to define the List Monad, but I know it when I see it...
function mapConcat (choices) {
return function (list) {
return _.map(choices, function (choice) {
return list.concat([choice])
})
}
}
function flatMapWith (fn, list) {
return _.reduce(list, function (acc, element) {
return acc.concat(fn(element));
}, [])
}
function unit (choices) {
return _.map(choices, function (choice) {
return [choice];
});
}
function forChoices (choices) {
var chooser = _.partial(flatMapWith, mapConcat(choices));
return _.compose(chooser, chooser, chooser)(unit(choices))
}
function all (generation) {
if (generation === 0) {
return [Cell(0), Cell(1)];
}
else return forChoices(all(generation - 1));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment