4 ms·
Why not just: function and(a,b) { return (a ? b : false) }
by karavelov 11y ago
Why not just:
function and(a,b) { return (a ? b : false) }
- qwertyuiop924 11y agoStill won't work. See comment above for why.
- karavelov 11y agoYes, I posted before seeing the other comments, the problem is that you can't force lazy evaluation of the function arguments in javascript. On the other hands it can be as easily written in languages that support it, for example in Scala: def and(a: Boolean, b: =>Boolean) = if (a) b else false And with Scala it can be easily made to work as an infix operator: implicit class AndOp(a: Boolean) { def and(b: =>Boolean) = if (a) b else false } Verification: def foo = { println("foo"); false } scala> false and foo res2: Boolean = false scala> true and foo foo res3: Boolean = false
- qwertyuiop924 11y agoHey, neat. Now can you rewrite the code you call and with, inside of and? I kid.