Created
August 30, 2011 22:14
-
-
Save juandopazo/1182244 to your computer and use it in GitHub Desktop.
JavaScript without logical operators or loops
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 _if(truthy, then) { | |
[then, function () {}][+!truthy](); | |
} | |
function ifElse(truthy, then, _else) { | |
[then, _else][+!truthy](); | |
} | |
function and(a, b) { | |
return !!((!!a + !!b) >> 1); | |
} | |
function or(a, b) { | |
return !!(!!a + !!b); | |
} | |
function _for(from, cond, augment, fn) { | |
var i = from; | |
_if(cond(i), function step() { | |
fn(i); | |
i = augment(i); | |
_if(cond(i), step); | |
}); | |
} | |
function _while(cond, fn) { | |
_if(cond(), function () { | |
fn(); | |
_while(cond, fn); | |
}); | |
} |
Me acordé de esto de casualidad. Corregí el or
y el and
.
and
ahora no usa >=. (!!a + !!b)
primero pasa a y b a boolean y la suma los pasa a numeros 0 o 1. La suma de eso puede valer 0, 1 o 2. En binario esto es 0, 1 o 10. El operador >> mueve el numero binario a la derecha poniendo un cero en su lugar. Es decir, 0 >> 1 == 0, 1 >> 1 == 0 y 10 >> 1 == 1. Eso lo vuelvo a pasar a boolean y voila!
eaa! se podía jaja congratz
it's great
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
y ahora sin usar < == > ?? :P