Home > Software design >  Progress bars using broadcast notation in Julia?
Progress bars using broadcast notation in Julia?

Time:11-23

In Julia, some amazing packages like ProgressMeter provide an easy API for displaying progress bars in the REPL. However, it seems like they only work with regular for loops, comprehensions and map/reduce. So, e.g., these work:

using ProgressMeter

function sleepyAdd(arg::int64)
    sleep(1)
    return arg   1000
end

nums = [1, 2, 3, 4]

# Progress bar with for loop
@showprogress for num in nums
    sleepyAdd(num)
end

# Progress bar with comprehension
@showprogress [sleepyAdd(num) for num in nums]

Howeve, you can't just use that with broadcast notation (so @showprogress sleepyAdd.(nums) doesn't work), which is a shame because brodcasting functions is one of the best features of Julia's syntax: elegant, simple and concise.

Is there any way around this limitation?

CodePudding user response:

The convenient macro @showprogress doesn't work in this case. But doing @macroexpand @showprogress... gives what is needed to get a working version for specific uses (and maybe for someone to update ProgressMeter.jl):

let t_nums = nums, 
    t_meter = ProgressMeter.Progress(ProgressMeter.length(t_nums)),
    t_wrapper = ProgressMeter.ProgressWrapper(t_nums, t_meter)
    # now comes the comprehension
    [sleepyAdd(num) for num in t_wrapper]
end

And you get the same effect as the for loop.

CodePudding user response:

I don't think there is - here's a link to the only mention of broadcasting in ProgressMeter's issues:

https://github.com/timholy/ProgressMeter.jl/issues/31#issuecomment-448558683

in which Tim says (in 2018) that he's not aware of anyone working on it.

  • Related