Skip to content

Instantly share code, notes, and snippets.

@mattneary
Created January 4, 2012 23:54
Show Gist options
  • Save mattneary/1562918 to your computer and use it in GitHub Desktop.
Save mattneary/1562918 to your computer and use it in GitHub Desktop.
Native Operators recreated
function range(start,end) {
return Array(end-start+1).join(0).split(0).map( function(v,i) {
return i+start;
});
}
function if_condition(bool,body,else_call) {
return bool()&&body() || else_call();
}
function while_loop(bool,body) {
var br = 0;
(function() {
body();
return bool()&&!br&&arguments.callee();
})();
}
function for_loop(range,body) {
var br = 0;
range.map(function(i) {
return br?0:body();
});
}
function switch_condition(condition,callbacks) {
return callbacks[condition()] || callbacks.default;
}
function switch_condition(a,b){return b[a()]||b.default}function for_loop(a,b){var c=0;a.map(function(a){return c?0:b()})}function while_loop(a,b){var c=0;(function(){b();return a()&&!c&&arguments.callee()})()}function if_condition(a,b,c){return a()&&b()||c()}function range(a,b){return Array(b-a+1).join(0).split(0).map(function(b,c){return c+a})}
@mattneary
Copy link
Author

I just had the idea of recreating for loops with an array of incrementing numbers and the map function. e.g.,

var sumFromOneToTen = 0;
range(1,10).map(function(i) {
   sumFromOneToTen += i; 
});

For lack of a better solution I used .join().split() on [undefined, undefined, undefined, undefined, undefined] because it was messing with the map function. I'm not sure what was going on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment