Created
July 8, 2013 18:19
-
-
Save puffnfresh/5951163 to your computer and use it in GitHub Desktop.
made with requirebin.com
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
var State = require('fantasy-states'), | |
Tuple2 = require('fantasy-tuples').Tuple2, | |
// Tuple2 Number Number | |
initial = Tuple2(0, 1), | |
// State (Tuple2 Number Number) Number -> State (Tuple2 Number Number) Number | |
next = discard( | |
State.modify(function(t) { | |
return Tuple2(t._1 + 1, (t._1 + 1) * t._2); | |
}), | |
State.get.map(snd) | |
), | |
result = factorial(5); | |
// Tuple2 a b -> b | |
function snd(t) { | |
return t._2; | |
} | |
// (m a, m b) -> m b | |
function discard(a, b) { | |
return a.chain(function(_) { | |
return b; | |
}); | |
} | |
// Array (m a) -> m a | |
function sequence(actions) { | |
var r = actions[0], | |
i; | |
for(i = 1; i < actions.length; i++) { | |
r = discard(r, actions[i]); | |
} | |
return r; | |
} | |
// (Number, a) -> Array a | |
function replicate(n, x) { | |
var r = [], | |
i; | |
for(i = 0; i < n; i++) { | |
r.push(x); | |
} | |
return r; | |
} | |
// Number -> Number | |
function factorial(n) { | |
return sequence(replicate(n, next)).eval(initial); | |
} | |
// Side-effect! | |
if(typeof document != 'undefined') { | |
document.body.innerHTML = result; | |
} else { | |
console.log(result); | |
} |
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
function snd(t){return t._2}function discard(t,n){return t.chain(function(){return n})}function sequence(t){var n,r=t[0];for(n=1;t.length>n;n++)r=discard(r,t[n]);return r}function replicate(t,n){var r,e=[];for(r=0;t>r;r++)e.push(n);return e}function factorial(t){return sequence(replicate(t,next)).eval(initial)}require=function(t,n,r){function e(r,u){if(!n[r]){if(!t[r]){var i="function"==typeof require&&require;if(!u&&i)return i(r,!0);if(o)return o(r,!0);throw Error("Cannot find module '"+r+"'")}var c=n[r]={exports:{}};t[r][0].call(c.exports,function(n){var o=t[r][1][n];return e(o?o:n)},c,c.exports)}return n[r].exports}for(var o="function"==typeof require&&require,u=0;r.length>u;u++)e(r[u]);return e}({"fantasy-states":[function(t,n){n.exports=t("KN4SDL")},{}],KN4SDL:[function(t,n){var r=t("fantasy-tuples").Tuple2,e=t("daggy"),o=e.tagged("run");o.of=function(t){return o(function(n){return r(t,n)})},o.prototype.chain=function(t){var n=this;return o(function(r){var e=n.run(r);return t(e._1).run(e._2)})},o.get=o(function(t){return r(t,t)}),o.modify=function(t){return o(function(n){return r(null,t(n))})},o.put=function(t){return o.modify(function(){return t})},o.prototype.eval=function(t){return this.run(t)._1},o.prototype.exec=function(t){return this.run(t)._2},o.prototype.map=function(t){return this.chain(function(n){return o.of(t(n))})},o.prototype.ap=function(t){return this.chain(function(n){return t.map(n)})},o.StateT=function(t){var n=e.tagged("run");return n.lift=function(t){return n(function(){return t})},n.of=function(e){return n(function(n){return t.of(r(e,n))})},n.prototype.chain=function(){var t=this;return n(function(n){t.run(n)})},n.get=n(function(n){return t.of(r(n,n))}),n.modify=function(e){return n(function(n){return t.of(r(null,e(n)))})},n.put=function(t){return n.modify(function(){return t})},n.prototype.eval=function(t){return this.run(t).chain(function(t){return t._1})},n.prototype.exec=function(t){return this.run(t).chain(function(t){return t._2})},n.prototype.map=function(t){return this.chain(function(r){return n.of(t(r))})},n.prototype.ap=function(t){return this.chain(function(n){return t.map(n)})},n},n!==void 0&&(n.exports=o)},{"fantasy-tuples":1,daggy:2}],2:[function(t,n,r){(function(){(function(t,n){"use strict";"function"==typeof define&&define.amd?define(["exports"],n):r!==void 0?n(r):(t.daggy={},n(t.daggy))})(this,function(t){function n(t){function n(){}return n.prototype=t,new n}function r(t,r){return t instanceof r?t:n(r.prototype)}function e(){function t(){var e,o=r(this,t);if(arguments.length!=n.length)throw new TypeError("Expected "+n.length+" arguments, got "+arguments.length);for(e=0;n.length>e;e++)o[n[e]]=arguments[e];return o}var n=[].slice.apply(arguments);return t._length=n.length,t}function o(t){function r(){throw new TypeError("Tagged sum was called instead of one of its properties.")}function o(n){return function(r){var e,o=t[n],u=[];if(!r[n])throw new TypeError("Constructors given to cata didn't include: "+n);for(e=0;o.length>e;e++)u.push(this[o[e]]);return r[n].apply(this,u)}}function u(t){var e=n(r.prototype);return e.cata=o(t),e}var i;for(i in t)t[i].length?(r[i]=e.apply(null,t[i]),r[i].prototype=u(i)):r[i]=u(i);return r}t.create=n,t.getInstance=r,t.tagged=e,t.taggedSum=o})})()},{}],1:[function(t,n,r){var e=t("daggy"),o=e.tagged("_1","_2"),u=e.tagged("_1","_2","_3"),i=e.tagged("_1","_2","_3","_4"),c=e.tagged("_1","_2","_3","_4","_5");o.prototype.concat=function(t){return o(this._1.concat(t._1),this._2.concat(t._2))},u.prototype.concat=function(t){return u(this._1.concat(t._1),this._2.concat(t._2),this._3.concat(t._3))},i.prototype.concat=function(t){return i(this._1.concat(t._1),this._2.concat(t._2),this._3.concat(t._3),this._4.concat(t._4))},c.prototype.concat=function(t){return c(this._1.concat(t._1),this._2.concat(t._2),this._3.concat(t._3),this._4.concat(t._4),this._5.concat(t._5))},r!==void 0&&(r.Tuple2=o,r.Tuple3=u,r.Tuple4=i,r.Tuple5=c)},{daggy:2}]},{},[]),require=function(t,n,r){function e(r,u){if(!n[r]){if(!t[r]){var i="function"==typeof require&&require;if(!u&&i)return i(r,!0);if(o)return o(r,!0);throw Error("Cannot find module '"+r+"'")}var c=n[r]={exports:{}};t[r][0].call(c.exports,function(n){var o=t[r][1][n];return e(o?o:n)},c,c.exports)}return n[r].exports}for(var o="function"==typeof require&&require,u=0;r.length>u;u++)e(r[u]);return e}({"fantasy-tuples":[function(t,n){n.exports=t("UWk06O")},{}],UWk06O:[function(t,n,r){var e=t("daggy"),o=e.tagged("_1","_2"),u=e.tagged("_1","_2","_3"),i=e.tagged("_1","_2","_3","_4"),c=e.tagged("_1","_2","_3","_4","_5");o.prototype.concat=function(t){return o(this._1.concat(t._1),this._2.concat(t._2))},u.prototype.concat=function(t){return u(this._1.concat(t._1),this._2.concat(t._2),this._3.concat(t._3))},i.prototype.concat=function(t){return i(this._1.concat(t._1),this._2.concat(t._2),this._3.concat(t._3),this._4.concat(t._4))},c.prototype.concat=function(t){return c(this._1.concat(t._1),this._2.concat(t._2),this._3.concat(t._3),this._4.concat(t._4),this._5.concat(t._5))},r!==void 0&&(r.Tuple2=o,r.Tuple3=u,r.Tuple4=i,r.Tuple5=c)},{daggy:1}],1:[function(t,n,r){(function(){(function(t,n){"use strict";"function"==typeof define&&define.amd?define(["exports"],n):r!==void 0?n(r):(t.daggy={},n(t.daggy))})(this,function(t){function n(t){function n(){}return n.prototype=t,new n}function r(t,r){return t instanceof r?t:n(r.prototype)}function e(){function t(){var e,o=r(this,t);if(arguments.length!=n.length)throw new TypeError("Expected "+n.length+" arguments, got "+arguments.length);for(e=0;n.length>e;e++)o[n[e]]=arguments[e];return o}var n=[].slice.apply(arguments);return t._length=n.length,t}function o(t){function r(){throw new TypeError("Tagged sum was called instead of one of its properties.")}function o(n){return function(r){var e,o=t[n],u=[];if(!r[n])throw new TypeError("Constructors given to cata didn't include: "+n);for(e=0;o.length>e;e++)u.push(this[o[e]]);return r[n].apply(this,u)}}function u(t){var e=n(r.prototype);return e.cata=o(t),e}var i;for(i in t)t[i].length?(r[i]=e.apply(null,t[i]),r[i].prototype=u(i)):r[i]=u(i);return r}t.create=n,t.getInstance=r,t.tagged=e,t.taggedSum=o})})()},{}]},{},[]);var State=require("fantasy-states"),Tuple2=require("fantasy-tuples").Tuple2,initial=Tuple2(0,1),next=discard(State.modify(function(t){return Tuple2(t._1+1,(t._1+1)*t._2)}),State.get.map(snd)),result=factorial(5);"undefined"!=typeof document?document.body.innerHTML=result:console.log(result); |
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
<style type='text/css'>html, body { margin: 0; padding: 0; border: 0; } | |
body, html { height: 100%; width: 100%; }</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment