Last active
December 18, 2015 14:59
-
-
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...
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 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