Skip to content

Instantly share code, notes, and snippets.

@NobukazuHanada
Last active August 29, 2015 14:00
Show Gist options
  • Save NobukazuHanada/11322564 to your computer and use it in GitHub Desktop.
Save NobukazuHanada/11322564 to your computer and use it in GitHub Desktop.
var Switch = function(expr){
return new YetSucceed(expr);
}
var Succeed = { case : function(cond,proc){ return this; } };
var YetSucceed = function(expr){ this.expr = expr; };
YetSucceed.prototype.case = function(cond, proc){
if(cond() === this.expr){
proc();
return Succeed;
}
return this;
}
var x = 0;
new Switch(true).
case(function(){ return x > 0; },
function(){
console.log(x + " is positive.");
}).
case(function(){ return x === 0; },
function(){
console.log(x + " is zero. ");
}).
case(function(){ return x < 0; },
function(){
console.log(x + " is negative . ");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment