Live data from Hacker News

Why 1 && 2 == 2

blog.chewxy.com

31–40 of 58 posts

Re: Why 1 && 2 == 2

#31

I like how cout demonstrates the lameness of the whole C++ iostreams syntax. I'll have to remember that the next time an argument arises.

Feel free to change it to how you like it if you feel that another syntax would be more convenient. You can choose another operator or just create a variadic template function and use function syntax to avoid operator precedence issues.

Re: Why 1 && 2 == 2

#32
post #17
post #15

Earlier quoted context omitted.

Or didn't put a format specifier, which makes sense coming from languages that don't have one in their print statements.

Not putting a format specifier wouldn't cause a segfault.

[deleted]

Re: Why 1 && 2 == 2

#34
post #33

Worth it for the memoization (I guess that's not really a word?) in Ruby. Cool new operator I did not know about.

It's not a new operator, but rather the X= form, where `foo X= 4` is sugar for `foo = foo X 4`

Just be careful with this in JavaScript, as the greater number of things that are falsy can bite you if you do `a = a || 10;`, which is a very common JS bug.

Re: Why 1 && 2 == 2

#35
post #32
post #17

Earlier quoted context omitted.

Not putting a format specifier wouldn't cause a segfault.

[deleted]

Well, there is a difference between "format specifier" and "format string". The first argument to printf is the format string, the `%d` and `%s` type things are the format specifiers. I was talking about this

    printf("Hi", 45);
which will not segfault. If you meant

    printf(45);
Then you are absolutely correct, that would segfault on common machines.

Re: Why 1 && 2 == 2

#36
post #19

For Python, a common idiom is to do this: condition and truecase or falsecase See "Boolean operations" and "Conditional expressions": http://docs.python.org/3/reference/expressions.html#boolean-...

This idiom is however out of flavor, unless you need to support python 2.4. Nowadays you would do this: truecase if condition else falsecase

Heh; my "age" is starting to show. The first version of Python I learned was Python 2.4 and it was only a few years ago I finally moved to 2.6. Skipping 2.7 and moving to 3.3+ soon.

Personally, I prefer the brevity of the older syntax, but to each his own. However, I understand the "ternary" expression was added because the older syntax was error-prone:

http://mail.python.org/pipermail/python-dev/2005-September/0... http://www.python.org/dev/peps/pep-0308/

Re: Why 1 && 2 == 2

#38

Once you understand short-circuit evaluation, it opens up certain coding tricks. The line "[operation] or [error condition]" will attempt the operation on the left. If it succeeds, the program continues on the next line, ignoring the part about the error condition. If it fails, the error condition will execute instead. This might be something like "open file or die". Likewise, "[operation] and [success condition]" wi…

For fun, sometimes I'll rewrite JS trying not to use any explicit conditionals (keywords if/else/), and use short-circuiting instead. It's a fun exercise but I know better than to commit the code, I think I'd be dead several times over by now...

Re: Why 1 && 2 == 2

#39

For Python, a common idiom is to do this: condition and truecase or falsecase See "Boolean operations" and "Conditional expressions": http://docs.python.org/3/reference/expressions.html#boolean-...

This is how you do it in Lua, as well, since it doesn't have a ternary operator.

Re: Why 1 && 2 == 2

#40
post #33

Worth it for the memoization (I guess that's not really a word?) in Ruby. Cool new operator I did not know about.

Watch out for this with boolean values. If you are attempting to memoize a method that returns a boolean, it'll be re-evaluated if the last iteration returned false.
Post reply on HN