Golfing Logical Operations in ES6
"GLOE (S6)"
General Logic
Say you have constructed statements s
and t
. See if you can use any of the following replacements:
Traditional conjuction: s&&t
Equivalent conjuction: s*t
Traditional disjunction: s||t
Equivalent disjunction: s+t
(These may not work if the order is wrong; i.e. +
and *
have a lower order precedence than ||
and &&
do.)
Also, here are some handy logical expressions:
- Either
s
or t
is true/XOR: s^t
s
and t
are the same truth value: !s^t
or s==t
Array logic
All members of a
satisfy condition p
:
a.every(p) // 10 bytes (11 bytes saved)
a.map(x=>c&=p(x),c=1) // 21 bytes (16 bytes saved)
for(i=0,c=1;i<a.length;c&=p(a[i++])); // 37 bytes (hideously long)
At least one member of a
satisfies condition p
:
a.some(p) // 9 bytes (13 bytes saved)
a.map(x=>c|=p(x),c=0) // 21 bytes (14 bytes saved)
for(i=c=0;i<a.length;c|=p(a[i++])); // 35 bytes (just please no)
No members of a
satisfy condition p
: !a.some(p)
.