Created
January 4, 2012 23:54
-
-
Save mattneary/1562918 to your computer and use it in GitHub Desktop.
Native Operators recreated
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 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; | |
} |
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 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})} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I just had the idea of recreating for loops with an array of incrementing numbers and the map function. e.g.,
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.