short-circuiting

When to prefer `and` over `andalso` in guard tests

亡梦爱人 提交于 2019-12-03 09:19:07
问题 I am curious why the comma ‹,› is a shortcut for and and not andalso in guard tests. Since I'd call myself a “C native” I fail to see any shortcomings of short-circuit boolean evaluation. I compiled some test code using the to_core flag to see what code is actually generated. Using the comma, I see the left hand value and right and value get evaluated and both and'ed. With andalso you have a case block within the case block and no call to erlang:and/2 . I did no benchmark tests but I daresay

Why does multiplication only short circuit on one side

橙三吉。 提交于 2019-12-03 08:06:11
问题 I was messing around with fix and after messing around with it I came across some weird behavior, namely that 0 * undefined is *** Exception: Prelude.undefined and undefined * 0 is 0 . Which also means that fix (0 *) is *** Exception: <<loop>> and fix (* 0) is 0 . After playing around with it it seems like the reason is because it is non-trivial to make it short circuit in both directions, as that doesn't really make much sense, without some sort of weird parallel computation and start with

Can I force my own short-circuiting in a method call?

丶灬走出姿态 提交于 2019-12-03 08:00:57
Suppose I want to check a bunch of objects to make sure none is null: if (obj != null && obj.Parameters != null && obj.Parameters.UserSettings != null) { // do something with obj.Parameters.UserSettings } It is an alluring prospect to write a helper function to accept a variable number of arguments and simplify this kind of check: static bool NoNulls(params object[] objects) { for (int i = 0; i < objects.Length; i++) if (objects[i] == null) return false; return true; } Then the above code could become: if (NoNulls(obj, obj.Parameters, obj.Parameters.UserSettings)) { // do something } Right?

Short-circuit evaluation like Python's “and” while storing results of checks

梦想与她 提交于 2019-12-03 04:27:11
I have multiple expensive functions that return results. I want to return a tuple of the results of all the checks if all the checks succeed. However, if one check fails I don't want to call the later checks, like the short-circuiting behavior of and . I could nest if statements, but that will get out of hand if there are a lot of checks. How can I get the short-circuit behavior of and while also storing the results for later use? def check_a(): # do something and return the result, # for simplicity, just make it "A" return "A" def check_b(): # do something and return the result, # for

Any difference between Lazy evaluation and Short-circuit evaluation?

自作多情 提交于 2019-12-03 02:56:56
From Wikipedia: Lazy evaluation is: In programming language theory, lazy evaluation or call-by-need is an evaluation strategy which delays the evaluation of an expression until its value is needed Short-circuit evaluation is: Short-circuit evaluation, minimal evaluation, or McCarthy evaluation denotes the semantics of some Boolean operators in some programming languages in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression So what's the difference between them for example when I have: if(false && true && true)

Java 8 stream short-circuit

女生的网名这么多〃 提交于 2019-12-03 01:41:34
Reading up a bit on Java 8, I got to this blog post explaining a bit about streams and reduction of them, and when it would be possible to short-circuit the reduction. At the bottom it states: Note in the case of findFirst or findAny we only need the first value which matches the predicate (although findAny is not guaranteed to return the first). However if the stream has no ordering then we’d expect findFirst to behave like findAny . The operations allMatch , noneMatch and anyMatch may not short-circuit the stream at all since it may take evaluating all the values to determine whether the

How to make `short-circuit evaluation` also available in `fold expressions`?

五迷三道 提交于 2019-12-03 00:35:35
#include <type_traits> #define FORWARD(arg)\ std::forward<decltype(arg)>(arg) template<typename... Args> constexpr bool AndL(Args&&... args) { return (... && FORWARD(args)); } template<typename... Args> constexpr bool AndR(Args&&... args) { return (FORWARD(args) && ...); } int main() { bool* pb = nullptr; false && (*pb = true); // ok at runtime. AndL(false, (*pb = true)); // error at runtime! AndR(false, (*pb = true)); // error at runtime! } The traditional && operator supports short-circuit evaluation , so false && (*pb = true) will be ok at runtime, but the following two cases are not. How

Why does multiplication only short circuit on one side

微笑、不失礼 提交于 2019-12-02 21:37:47
I was messing around with fix and after messing around with it I came across some weird behavior, namely that 0 * undefined is *** Exception: Prelude.undefined and undefined * 0 is 0 . Which also means that fix (0 *) is *** Exception: <<loop>> and fix (* 0) is 0 . After playing around with it it seems like the reason is because it is non-trivial to make it short circuit in both directions, as that doesn't really make much sense, without some sort of weird parallel computation and start with the first non-bottom returned. Is this kind of thing seen in other places (reflexive functions that aren

Understanding Bash short-circuiting

こ雲淡風輕ζ 提交于 2019-12-02 17:45:55
问题 First of all I'm not a Bash pro. I discovered few months ago that if I use both the && and || short circuit operators in sequence with curly braces, then in case the first statement exits with a truthful value, if the last statement in the true block exits non-zero, then the fail block will be executed too. Like this: returnNumber 0 && { echo 'OK' returnNumber 1 } || { echo 'NG' } Will output: OK NG So, I looked for the easiest solution for this, and came up with this: returnNumber 0 && {

Bitwise AND (&) expression in Java

混江龙づ霸主 提交于 2019-12-02 11:04:38
问题 I am debugging code that has in it expr1 & expr2 where expr1 has a side effect that affects expr2 evaluation result. I suspect that expr2 gets evaluated before expr1 , since JLS guarantees left-to-right evaluation for && , but not necessarily for & . I also suspect that change of evaluation order may be a result of optimization performed by HotSpot (we're running Java 6u20). Do you know if HotSpot can make such an optimization? Better yet, provide any pointers to documentation that either