Skip to content

Instantly share code, notes, and snippets.

@foxdonut
Last active December 14, 2015 01:11
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 foxdonut/10ed2581d5fd210fe615 to your computer and use it in GitHub Desktop.
Save foxdonut/10ed2581d5fd210fe615 to your computer and use it in GitHub Desktop.
requirebin sketch
// require() some stuff from npm (like you were using browserify)
// and then hit Run Code to run it on the right
var Maybe = require("data.maybe");
var f1 = function(input) {
console.log("f1");
return null;
};
var f2 = function(input) {
console.log("f2");
return 42;
};
var f3 = function(input) {
console.log("f3");
return null;
};
var input = "input";
var toMaybe = function(f) { return function() { return Maybe.fromNullable(f()); } };
var result = Maybe.fromNullable(f1(input)).orElse(toMaybe(function() { return f2(input); })).orElse(toMaybe(function() { return f3(input); })).get();
console.log("result:", result);
require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){module.exports=Maybe;var clone=Object.create;var unimplemented=function(){throw new Error("Not implemented.")};var noop=function(){return this};function Maybe(){}Just.prototype=clone(Maybe.prototype);function Just(a){this.value=a}Nothing.prototype=clone(Maybe.prototype);function Nothing(){}Maybe.Nothing=function(){return new Nothing};Maybe.prototype.Nothing=Maybe.Nothing;Maybe.Just=function(a){return new Just(a)};Maybe.prototype.Just=Maybe.Just;Maybe.fromNullable=function(a){return a!=null?new Just(a):new Nothing};Maybe.prototype.fromNullable=Maybe.fromNullable;Maybe.fromEither=function(a){return a.fold(Maybe.Nothing,Maybe.Just)};Maybe.prototype.fromEither=Maybe.fromEither;Maybe.fromValidation=Maybe.fromEither;Maybe.prototype.fromValidation=Maybe.fromEither;Maybe.prototype.isNothing=false;Nothing.prototype.isNothing=true;Maybe.prototype.isJust=false;Just.prototype.isJust=true;Maybe.of=function(a){return Maybe.prototype.Just(a)};Maybe.prototype.of=Maybe.of;Maybe.prototype.ap=unimplemented;Nothing.prototype.ap=noop;Just.prototype.ap=function(b){return b.map(this.value)};Maybe.prototype.map=unimplemented;Nothing.prototype.map=noop;Just.prototype.map=function(f){return this.of(f(this.value))};Maybe.prototype.chain=unimplemented;Nothing.prototype.chain=noop;Just.prototype.chain=function(f){return f(this.value)};Maybe.prototype.toString=unimplemented;Nothing.prototype.toString=function(){return"Maybe.Nothing"};Just.prototype.toString=function(){return"Maybe.Just("+this.value+")"};Maybe.prototype.isEqual=unimplemented;Nothing.prototype.isEqual=function(b){return b.isNothing};Just.prototype.isEqual=function(b){return b.isJust&&b.value===this.value};Maybe.prototype.get=unimplemented;Nothing.prototype.get=function(){throw new TypeError("Can't extract the value of a Nothing.")};Just.prototype.get=function(){return this.value};Maybe.prototype.getOrElse=unimplemented;Nothing.prototype.getOrElse=function(a){return a};Just.prototype.getOrElse=function(_){return this.value};Maybe.prototype.orElse=unimplemented;Nothing.prototype.orElse=function(f){return f()};Just.prototype.orElse=function(_){return this};Maybe.prototype.cata=unimplemented;Nothing.prototype.cata=function(pattern){return pattern.Nothing()};Just.prototype.cata=function(pattern){return pattern.Just(this.value)};Maybe.prototype.toJSON=unimplemented;Nothing.prototype.toJSON=function(){return{"#type":"folktale:Maybe.Nothing"}};Just.prototype.toJSON=function(){return{"#type":"folktale:Maybe.Just",value:this.value}}},{}],"data.maybe":[function(require,module,exports){module.exports=require("./maybe")},{"./maybe":1}]},{},[]);var Maybe=require("data.maybe");var f1=function(input){console.log("f1");return null};var f2=function(input){console.log("f2");return 42};var f3=function(input){console.log("f3");return null};var input="input";var toMaybe=function(f){return function(){return Maybe.fromNullable(f())}};var result=Maybe.fromNullable(f1(input)).orElse(toMaybe(function(){return f2(input)})).orElse(toMaybe(function(){return f3(input)})).get();console.log("result:",result);
{
"name": "requirebin-sketch",
"version": "1.0.0",
"dependencies": {
"data.maybe": "1.2.1"
}
}
<!-- contents of this file will be placed inside the <body> -->
<!-- contents of this file will be placed inside the <head> -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment