Created
April 16, 2013 11:00
-
-
Save dtchepak/5395077 to your computer and use it in GitHub Desktop.
Code used while exploring examples for http://davesquared.net/2013/01/yamt.html
This file contains hidden or 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
| //monads.js | |
| // | |
| exports.bestSellingCategories = function () { | |
| return [ createCat(42, "MOL", false) | |
| , createCat(101, "Both kinds", false) | |
| , createCat(418, "http", true) | |
| ]; | |
| } | |
| createCat = function(id,name,shh) { return { id: id, name: name, isSecret: shh}; } | |
| exports.productsInCategory = function (id) { | |
| if (id === 42) return ["earl grey", "english breakfast"]; | |
| if (id === 101) return ["country", "western"]; | |
| if (id === 418) return ["teapot"]; | |
| else return []; | |
| } | |
| exports.flatten = function(list) { | |
| return list.reduce(function (acc, x) { | |
| return acc.concat(x); | |
| }, []); | |
| } | |
| exports.bind = function(f, list) { | |
| return exports.flatten(list.map(f)); | |
| } | |
| Array.prototype.bind = (function() { | |
| return function(f) { return m.flatten(this.map(f)); } | |
| })(); | |
| exports.sample = function() { | |
| return exports.bestSellingCategories().bind(function(category) { | |
| return exports.productsInCategory(category.id).bind(function (product) { | |
| return [product + " " + exports.onSpecial(product, category)]; | |
| }); }); }; | |
| exports.sample2 = function() { | |
| return exports.bestSellingCategories().map(function(category) { | |
| return exports.productsInCategory(category.id).map(function (product) { | |
| return (product + " " + exports.onSpecial(product, category)); | |
| }); }); }; | |
| unit = function(x) { return [x]; } | |
| exports.onSpecial = function(product,category) { | |
| if (category.name === "MOL" || product === "western") return "[ON SALE!]"; | |
| return ""; | |
| } | |
| exports.inStock = function(product) { | |
| var isInStock = ['earl grey', 'western', 'teapot'].indexOf(product) >= 0; | |
| return isInStock ? "[in stock]" : "[sold out]"; | |
| } | |
| exports.sequence = function(m, list) { | |
| return list.reduce(function(acc, monad) { | |
| return m.bind(function(x) { | |
| return m.map(function (ms) { return ms.concat(x); }, acc); | |
| }, monad); | |
| }, m.unit([])); | |
| }; | |
| exports.lift2 = function(m, f, ma, mb) { | |
| return m.bind(function(a) { | |
| return m.bind(function(b) { return m.unit(f(a,b)); }, mb); | |
| }, ma); | |
| } | |
| exports.OptionMonad = { | |
| bind: function(f, m) { | |
| if (m.isEmpty) return m; | |
| else return f(m.value); | |
| }, | |
| unit: function(x) { return new exports.Option(x); }, | |
| } | |
| /* | |
| map: function(f, m) { | |
| if (m.isEmpty) return m; | |
| else return new exports.Option(f(m.value)); | |
| } | |
| */ | |
| exports.Option = (function() { | |
| return function(x) { | |
| var is_empty = typeof x == 'undefined' || x == null | |
| return { value: x, isEmpty: is_empty, toString: function() { | |
| if (is_empty) return "{Empty}"; | |
| else return "{Just " + x + "}"; | |
| } }; | |
| }; | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment