Timeline for Tips for golfing in <all languages>
Current License: CC BY-SA 4.0
16 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Aug 27 at 13:01 | comment | added | Wrzlprmft | @Lucenaposition: Ah, right, I forgot about comparison chaining. Still, not the point of this tip. | |
Aug 27 at 6:16 | comment | added | Lucenaposition |
@Wrzlprmft In python, 1>2>1/0 does not cause an error. The 1>2 evaluates to false so the 1/0 is ignored. You can see this as 1/0 would cause an error if evaluated.
|
|
Aug 27 at 5:34 | history | edited | Wrzlprmft | CC BY-SA 4.0 |
added 159 characters in body
|
Aug 27 at 5:34 | comment | added | Wrzlprmft |
@Lucenaposition: In all programming languages I know, this would not work as you describe. First, A>B gets evaluated and no matter the outcome, foo() would be evaluated to compare it to that outcome.
|
|
Aug 27 at 2:50 | comment | added | Lucenaposition |
You can do A>B==foo() which evaluates foo only when A>B . If foo returns something that can be compares with a boolean, you can use A>B>foo() .
|
|
Dec 28, 2022 at 20:42 | comment | added | Joao-3 |
B>A and foo() would evaluate foo if and only if B>A .
|
|
Sep 11, 2019 at 18:33 | comment | added | JayXon |
@Wrzlprmft I interpreted that as anything that evaluates to a boolean, like (1>0) or all([]) because in codegolf you want to inline as much as possible, so having a boolean variable is less common than expressions.
|
|
Sep 11, 2019 at 9:42 | comment | added | Wrzlprmft |
@JayXon: Hence “if A and B are booleans”. If A and B are expressions, numbers, etc., you have to be more careful.
|
|
Sep 11, 2019 at 8:46 | comment | added | JayXon |
Technically they are not equivalent, if A is false then B will not be evaluated in the first version.
|
|
Jul 8, 2016 at 7:41 | comment | added | Katenkyo | Also, if you have long imbricated conditions (say with 5/6 parameters), you can use a Truth table and a Karnaugh map to find the shortest boolean expression for it | |
Jun 19, 2016 at 7:54 | comment | added | msh210 |
@scragar, B>A or foo would evaluate foo if B==A which is not what we want. (Right?)
|
|
Feb 20, 2015 at 2:41 | history | wiki removed | Doorknob | ||
Sep 11, 2014 at 12:22 | comment | added | Wrzlprmft | @scragar: Correct, but this is not the point of this tip. (It is a valuable independent tip though.) | |
Sep 11, 2014 at 12:13 | comment | added | scragar |
B>A or foo() would be an even shorter way to express this, take advantage of lazy evaluation of boolean expressions to ensure it only calculates things when it needs to.
|
|
Aug 8, 2014 at 13:14 | comment | added | cjfaure | I'd never have thought of that. | |
Aug 8, 2014 at 13:03 | history | answered | Wrzlprmft | CC BY-SA 3.0 |