Thank you. The gist works well.

In situations when I have to use a for loop, is there a way to tell if the for loop has completed or not?

Wai Yip

Gray Calhoun <mailto:[email protected]>
Friday, January 30, 2015 3:25 PM
You can use a macro. I've written short-circuiting 'any' and 'all' macros that are available in this gist:

https://gist.github.com/grayclhn/5e70f5f61d91606ddd93

I'm sure they can be substantially improved; the usage would be

||
if@all[f(x)forx in1:1000000000000000000000000000000000]
## success
else
## failure
end

and it rewrites the list comprehension as a loop and inserts a break statement

On Friday, January 30, 2015 at 12:51:13 AM UTC-6, Wai Yip Tung wrote:
Wai Yip Tung <mailto:[email protected]>
Thursday, January 29, 2015 10:51 PM
I want to apply function f() over a range of value. f() returns true for success and false for failure. Since f() is expensive, I want short circuit computation, i.e. it stops after the first failure.

In python, I can do this in an elegant way with the all() function and generator expression.

if all(f(x) for x in values)
  # success
else
  # failure

From what I understand, there is no generator expression in Julia. List comprehension will evaluate the full list. Even if I try to use for loop, I can't use the control variable to check if the loop has run to finish or not.

i = 0
for i in 1:length(values)
    if !f(values[i])
      break
   end
end
# The status is ambiguous if i==length(values)

My last resort is to add flags to indicate if is success or not. Is there some more elegant way to do this?

Reply via email to