Does the VBA And operator evaluate the second argument when the first is false?
What you are looking for is called "short-circuit evaluation".
VBA doesn't have it.
You can see an approach that is probably adaptable to your situation here.
The approach that was chosen there involved substituting a Select Case
for the If
. There is also an example of using nested Ifs
.
Does coroutines maintain order in if conditions?
First of all, it's bit non-sensical for a suspend function to have blocking
in it's name, since by convention, suspend
functions must never block. See the Suspending Convention section in this article by the design lead of Kotlin coroutines.
But, regardless of whether the function is a suspend function or if it blocks or not, code will execute synchronously, just as if this were not in a coroutine. In your example, myBlockingCoroutineFunction()
in the if condition will be executed, and after it returns, the next part of the Boolean expression will be evaluated.
Within the same coroutine, your myCondition
variable or property has an order guarantee and no synchronization is needed. If it's modified from multiple different coroutines simultaneously, depending on your use case, you might need to use mutual exclusion to protect the order of events.
Get the first item from an iterable that matches a condition
Python 2.6+ and Python 3:
If you want StopIteration
to be raised if no matching element is found:
next(x for x in the_iterable if x > 3)
If you want default_value
(e.g. None
) to be returned instead:
next((x for x in the_iterable if x > 3), default_value)
Note that you need an extra pair of parentheses around the generator expression in this case − they are needed whenever the generator expression isn't the only argument.
I see most answers resolutely ignore the next
built-in and so I assume that for some mysterious reason they're 100% focused on versions 2.5 and older -- without mentioning the Python-version issue (but then I don't see that mention in the answers that do mention the next
built-in, which is why I thought it necessary to provide an answer myself -- at least the "correct version" issue gets on record this way;-).
Python <= 2.5
The .next()
method of iterators immediately raises StopIteration
if the iterator immediately finishes -- i.e., for your use case, if no item in the iterable satisfies the condition. If you don't care (i.e., you know there must be at least one satisfactory item) then just use .next()
(best on a genexp, line for the next
built-in in Python 2.6 and better).
If you do care, wrapping things in a function as you had first indicated in your Q seems best, and while the function implementation you proposed is just fine, you could alternatively use itertools
, a for...: break
loop, or a genexp, or a try/except StopIteration
as the function's body, as various answers suggested. There's not much added value in any of these alternatives so I'd go for the starkly-simple version you first proposed.
Pass condition into a function
You can also use as below;
#! /bin/bash
a(){
condition="(( \$num $2 $3 ))"
num=$1
while eval $condition
do
(( num++ ))
echo "increase num1:$num"
done
}
b(){
num1=1
num2=5
a $num1 '<' $num2
}
b
Why does Javascript's regex.exec() not always return the same value?
A JavaScript RegExp
object is stateful.
When the regex is global, if you call a method on the same regex object, it will start from the index past the end of the last match.
When no more matches are found, the index is reset to 0
automatically.
To reset it manually, set the lastIndex
property.
reg.lastIndex = 0;
This can be a very useful feature. You can start the evaluation at any point in the string if desired, or if in a loop, you can stop it after a desired number of matches.
Here's a demonstration of a typical approach to using the regex in a loop. It takes advantage of the fact that exec
returns null
when there are no more matches by performing the assignment as the loop condition.
var re = /foo_(\d+)/g,
str = "text foo_123 more text foo_456 foo_789 end text",
match,
results = [];
while (match = re.exec(str))
results.push(+match[1]);
DEMO: http://jsfiddle.net/pPW8Y/
If you don't like the placement of the assignment, the loop can be reworked, like this for example...
var re = /foo_(\d+)/g,
str = "text foo_123 more text foo_456 foo_789 end text",
match,
results = [];
do {
match = re.exec(str);
if (match)
results.push(+match[1]);
} while (match);
DEMO: http://jsfiddle.net/pPW8Y/1/
Is checking nil and empty for a set at the same time safe in Swift?
It is perfectly safe as it will execute the second statement only when the first one is true.
It's better to use:
guard let newValue = newValue // First unwrap it
where newValue.count > 0 else { // Then check if the unwrapped value has items
return // If not we don't continue
}
// From this point you don't need to use ? or ! to access newValue anymore
Always try to use safe patterns in your Swift code. To me casting with ! is a code smell and I would always try to replace it with a if let thing = thing or guard let thing = thing statement
Python: Empty string evaluated to 'false'
Python has a ternary operator:
def baseN(num,b):
return baseN(num // b, b) + "0123456789abcdefghijklmnopqrstuvwxyz"[num % b] if num else ""
Performance difference of if if vs if else if
Assuming simple types (in this case, I used int
) and no funny business (didn't redefine operator= for int), at least with GCC 4.6 on AMD64, there is no difference. The generated code is identical:
0000000000000000 <case_1>: 0000000000000040 <case_2>:
0: 85 ff test %edi,%edi 40: 85 ff test %edi,%edi
2: 74 14 je 18 <case_1+0x18> 42: 74 14 je 58 <case_2+0x18>
4: 83 ff 01 cmp $0x1,%edi 44: 83 ff 01 cmp $0x1,%edi
7: 74 27 je 30 <case_1+0x30> 47: 74 27 je 70 <case_2+0x30>
9: 83 ff 02 cmp $0x2,%edi 49: 83 ff 02 cmp $0x2,%edi
c: 74 12 je 20 <case_1+0x20> 4c: 74 12 je 60 <case_2+0x20>
e: 66 90 xchg %ax,%ax 4e: 66 90 xchg %ax,%ax
10: f3 c3 repz retq 50: f3 c3 repz retq
12: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1) 52: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1)
18: 31 c0 xor %eax,%eax 58: 31 c0 xor %eax,%eax
1a: e9 00 00 00 00 jmpq 1f <case_1+0x1f> 5a: e9 00 00 00 00 jmpq 5f <case_2+0x1f>
1f: 90 nop 5f: 90 nop
20: 31 c0 xor %eax,%eax 60: 31 c0 xor %eax,%eax
22: e9 00 00 00 00 jmpq 27 <case_1+0x27> 62: e9 00 00 00 00 jmpq 67 <case_2+0x27>
27: 66 0f 1f 84 00 00 00 nopw 0x0(%rax,%rax,1) 67: 66 0f 1f 84 00 00 00 nopw 0x0(%rax,%rax,1)
2e: 00 00 6e: 00 00
30: 31 c0 xor %eax,%eax 70: 31 c0 xor %eax,%eax
32: e9 00 00 00 00 jmpq 37 <case_1+0x37> 72: e9 00 00 00 00 jmpq 77 <case_2+0x37>
37: 66 0f 1f 84 00 00 00 nopw 0x0(%rax,%rax,1)
3e: 00 00
The extra instruction at the end of case_1 is just for padding (to get the next function aligned).
This isn't really surprising, figuring out that p isn't changed in that function is fairly basic optimization. If p could be changed (e.g., passed-by-reference or pointer to the various do_…
functions, or was a reference or pointer itself, so there could be an alias) then the behavior is different, and of course the generated code would be too.
In Spring Webflow how do I transition from a failed on-start evaluate
you don't always need <on-start>
. you can have a decision state as starting state, and check inside that decision state if your condition is true or false, if true then transition to your view-state or other state, if not transition to a end state with a view.
let's say you call your flow documentFlow.xml
inside this file, you put a decision-state like the following:
<input name="solutionId" type="long" />
<decision-state id="isDocumentFormSet">
<on-entry>
<evaluate expression="solutionCreateEditFlowHelper.findDocumentForEdit(solutionId)" result="flowScope.documentForm"/>
</on-entry>
<if test="documentForm == null" then="chooseDocument" else="noDocument"/>
</decision-state>
<end-state id="noDocument"/>
<view-state id="chooseDocument">
....
</view-state>
Related Topics
Utf-8 Problems While Reading CSV File with Fgetcsv
Aescrypt Decryption Between iOS and PHP
Subtracting a Certain Number of Hours, Days, Months or Years from Date
Using Header() to Rewrite Filename in Url for Dynamic PDF
Unexpected 'Use' (T_Use) When Trying to Use Composer
Does PHP's $_Request Method Have a Security Problem
Which Function in PHP Validate If the String Is Valid HTML
Send a JSON Object from Android to PHP Server with Post Method and Httpurlconnection
Certificate Error Using Imap in PHP
How to Replace the Deprecated Set_Magic_Quotes_Runtime in PHP
How to Remove Null Values from an Array
Laravel: General Error: 1615 Prepared Statement Needs to Be Re-Prepared