Earlier quoted context omitted.
Just for my curiosity's sake, would you mind posting the code you tried that segfaulted?
Yeah I'm dying to see the original C code but it isn't on the page anywhere, AFAICT. From the article, about the C version: "Mine hadn't worked, which I suspect is some sort of compiler issue." Yeah I'm going to go out on a limb and proclaim that there's no way this was due to a compiler "issue" in gcc. Not that gcc is perfect, but there's no way it is barfing on one of these handful of line programs that does nothin…
Why 1 && 2 == 2
21–30 of 58 posts
Re: Why 1 && 2 == 2
#22For 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-...
truecase if condition else falsecase
which is so inside-out that it hurts.
It didn't occur to me to use the boolean style - I guess I think of that as a perl idiom, and assumed that it would get sneered at in py-land
Re: Why 1 && 2 == 2
#23The 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]" will attempt the operation on the left, and if it succeeds, perform the operation on the right. This might be something like "read input and process it", which will perform the (possibly expensive) processing step only when input is received.
Code written in this style is sometimes, but not always, faster to execute. It's also sometimes, but not always, clearer.
See also http://en.wikipedia.org/wiki/Short-circuit_evaluation
Re: Why 1 && 2 == 2
#24http://www.lispworks.com/documentation/HyperSpec/Body/m_and....
Re: Why 1 && 2 == 2
#25Once 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…
launchMissiles() or die('failure');Re: Why 1 && 2 == 2
#26Earlier quoted context omitted.
Just for my curiosity's sake, would you mind posting the code you tried that segfaulted?
Yeah I'm dying to see the original C code but it isn't on the page anywhere, AFAICT. From the article, about the C version: "Mine hadn't worked, which I suspect is some sort of compiler issue." Yeah I'm going to go out on a limb and proclaim that there's no way this was due to a compiler "issue" in gcc. Not that gcc is perfect, but there's no way it is barfing on one of these handful of line programs that does nothin…
Parts of the blog post was written late last night (about 3 am), and parts of it was written on my commute to work. I tried to rewrite what I wrote last night and tried to compile it on my work computer, and all I got were warnings.
So... can't recreate. Sorry
Re: Why 1 && 2 == 2
#27 cout
demonstrates the lameness of the whole C++ iostreams syntax. I'll have to remember that the next time an argument arises.Re: Why 1 && 2 == 2
#28Thank god he didn't try php. echo 2 && 1; // 1 echo 1 && 2; // 1 echo 2 || 1; // 1 echo 1 || 2; // 1 echo 2 ?: 1; // 2 In php ?: is javascript's || Unfortunately there is no && that works correctly.
Re: Why 1 && 2 == 2
#29Re: Why 1 && 2 == 2
#30For 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-...
ah! I've been trying to write Python lately (I'm more of a Rubyist) and the internet suggested: truecase if condition else falsecase which is so inside-out that it hurts. It didn't occur to me to use the boolean style - I guess I think of that as a perl idiom, and assumed that it would get sneered at in py-land