perlancar has asked for the wisdom of the Perl Monks concerning the following question:
tqdm is a Python progress bar/meter library which has a simple interface: you just wrap an iterable:
for i in range(100): ...
with:
for i in tqdm(range(100)): ...
and voila, you have a progress bar. I wonder how we can do something similar in Perl. If we create a tqdm() function which returns a tied array, e.g.:
for (tqdm(1..100)) { ... }
then Perl will FETCH all the items first before starting the loop, defeating the progress measuring.
We can make tqdm() function return a tied scalar for every element, but that will be more inefficient and introduce additional side effects.
We can have tqdm() return a tied scalar, something like:
for (my $i = tqdm(100); $i < 100; $i++) { ... }
but: 1) $i is still a tied scalar; 2) C-style loop is less nice and less often used; 3) the above syntax is repetitive.
Any ideas?
UPDATE 2019-04-16: We did it! In Perl we can indeed support the same syntax by using a combination of tied array and lvalue wrapper. Read all about it in Getting for() to accept a tied array in one statement.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Porting tqdm to Perl (updated)
by haukex (Archbishop) on Dec 21, 2018 at 10:56 UTC | |
by perlancar (Hermit) on Dec 21, 2018 at 13:58 UTC | |
|
Re: Porting tqdm to Perl
by Corion (Patriarch) on Dec 21, 2018 at 15:46 UTC | |
|
Re: Porting tqdm to Perl
by LanX (Saint) on Dec 21, 2018 at 13:54 UTC | |
by perlancar (Hermit) on Dec 21, 2018 at 14:07 UTC | |
by LanX (Saint) on Dec 21, 2018 at 14:11 UTC | |
by perlancar (Hermit) on Dec 21, 2018 at 14:19 UTC | |
|
Re: Porting tqdm to Perl
by kcott (Archbishop) on Dec 22, 2018 at 09:15 UTC | |
by perlancar (Hermit) on Dec 24, 2018 at 00:08 UTC | |
|
Re: Porting tqdm to Perl
by LanX (Saint) on Dec 21, 2018 at 14:02 UTC |