view on requirebin
Last active
December 19, 2015 13:39
-
-
Save puffnfresh/5963363 to your computer and use it in GitHub Desktop.
requirebin sketch
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 daggy = require('daggy'), | |
State = require('fantasy-states'), | |
Cofree = require('fantasy-cofrees'), | |
AST = daggy.taggedSum({ | |
Add: ['l', 'r'], | |
Sub: ['l', 'r'], | |
Num: ['n'] | |
}), | |
incNode = State.get.chain(function(n) { | |
return discard( | |
State.modify(succ), | |
State.of(n) | |
); | |
}); | |
Cofree.prototype.toString = function() { | |
return 'Cofree(' + this.a + ', ' + this.f + ')'; | |
}; | |
function identity(a) { | |
return a; | |
} | |
function discard(a, b) { | |
return a.chain(function(_) { | |
return b; | |
}); | |
} | |
function succ(n) { | |
return n + 1; | |
} | |
AST.prototype.traverse = function(f, p) { | |
return this.cata({ | |
Add: function(l, r) { | |
return f(l).map(function(l) { | |
return function(r) { | |
return AST.Add(l, r); | |
}; | |
}).ap(f(r)); | |
}, | |
Sub: function(l, r) { | |
return f(l).map(function(l) { | |
return function(r) { | |
return AST.Sub(l, r); | |
}; | |
}).ap(f(r)); | |
}, | |
Num: function(n) { | |
return p.of(AST.Num(n)); | |
} | |
}); | |
}; | |
AST.prototype.toString = function() { | |
return this.cata({ | |
Add: function(l, r) { | |
return 'AST.Add(' + l + ', ' + r + ')'; | |
}, | |
Sub: function(l, r) { | |
return 'AST.Sub(' + l + ', ' + r + ')'; | |
}, | |
Num: function(n) { | |
return 'AST.Num(' + n + ')'; | |
} | |
}); | |
}; | |
function print(s) { | |
if(typeof document != 'undefined') { | |
document.body.innerHTML += '<pre>' + s + '</pre>'; | |
} else { | |
console.log(s); | |
} | |
} | |
print( | |
Cofree( | |
incNode, | |
AST.Add( | |
Cofree( | |
incNode, | |
AST.Add( | |
Cofree( | |
incNode, | |
AST.Num(1) | |
), | |
Cofree( | |
incNode, | |
AST.Num(2) | |
) | |
) | |
), | |
Cofree( | |
incNode, | |
AST.Num(3) | |
) | |
) | |
).traverse(identity, State).eval(0).toString() | |
); |
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 identity(t){return t}function discard(t,n){return t.chain(function(){return n})}function succ(t){return t+1}function print(t){"undefined"!=typeof document?document.body.innerHTML+="<pre>"+t+"</pre>":console.log(t)}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}({daggy:[function(t,n){n.exports=t("Cka9PM")},{}],Cka9PM:[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})})()},{}]},{},[]),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-cofrees":[function(t,n){n.exports=t("Ms/Tld")},{}],"Ms/Tld":[function(t,n){var r=t("daggy"),e=r.tagged("a","f");e.prototype.map=function(t){return e(t(this.a),this.f.map(function(n){return n.map(t)}))},e.prototype.extract=function(){return this.a},e.prototype.extend=function(t){return e(t(this),this.f.map(function(n){return n.extend(t)}))},e.prototype.traverse=function(t,n){function r(o){return t(o.a).map(function(t){return function(n){return e(t,n)}}).ap(o.f.traverse(r,n))}return r(this)},n!==void 0&&(n.exports=e)},{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 daggy=require("daggy"),State=require("fantasy-states"),Cofree=require("fantasy-cofrees"),AST=daggy.taggedSum({Add:["l","r"],Sub:["l","r"],Num:["n"]}),incNode=State.get.chain(function(t){return discard(State.modify(succ),State.of(t))});Cofree.prototype.toString=function(){return"Cofree("+this.a+", "+this.f+")"},AST.prototype.traverse=function(t,n){return this.cata({Add:function(n,r){return t(n).map(function(t){return function(n){return AST.Add(t,n)}}).ap(t(r))},Sub:function(n,r){return t(n).map(function(t){return function(n){return AST.Sub(t,n)}}).ap(t(r))},Num:function(t){return n.of(AST.Num(t))}})},AST.prototype.toString=function(){return this.cata({Add:function(t,n){return"AST.Add("+t+", "+n+")"},Sub:function(t,n){return"AST.Sub("+t+", "+n+")"},Num:function(t){return"AST.Num("+t+")"}})},print(""+Cofree(incNode,AST.Add(Cofree(incNode,AST.Add(Cofree(incNode,AST.Num(1)),Cofree(incNode,AST.Num(2)))),Cofree(incNode,AST.Num(3)))).traverse(identity,State).eval(0)); |
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