in reply to Re^7: Getting for() to accept a tied array in one statement
in thread Getting for() to accept a tied array in one statement

Yes, the goal is not finding out the best way to do a progress-bar API (I think doing the Progress::Any-style progress bar is still okay), but how to emulate the style of a Python library.

As a bonus, one doesn't need to know the length of the list in advance with an iterator!

With for(@tied_array) we also doesn't need to calculate the length of the list in advance. We can just retrieve an item one by one, and for() will still invoke FETCHSIZE on each iteration. Which incurs an extra cost, admittedly.

  • Comment on Re^8: Getting for() to accept a tied array in one statement

Replies are listed 'Best First'.
Re^9: Getting for() to accept a tied array in one statement
by ikegami (Patriarch) on Apr 22, 2019 at 21:56 UTC

    but how to emulate the style of a Python library.

    I presume Python's has native support for iterators. Perl 6 has lazy lists, but Perl 5's doesn't, and nothing's going to make for work with lazy lists short of overriding for/foreach. Tied arrays definitely can't achieve that.

    There are a few modules that provide iterator-aware looping primitives. Search for "iterator" on CPAN.

    With for(@tied_array) we also doesn't need to calculate the length of the list in advance

    That's not true.

    Whether it's called for every loop pass or not, it's still called before the first pass, so you still need to know the size up front. And FETCHSIZE must always return a correct value because it's called once in other situations.

    In fact, I consider it a bug that FETCHSIZE is called more than once in that situation.

    Ironically, if you circumvent the optimisation of for (@a) by using for ((), @a), you actually get something faster for tied arrays since the latter only calls FETCHSIZE once.

    BEGIN { package My::TiedArray; use strict; use warnings; use Tie::Array qw( ); our @ISA = 'Tie::StdArray'; sub TIEARRAY { my $class = shift; bless [@_], $class } sub FETCHSIZE { my $self = shift; print("FETCHSIZE\n"); return $self->SUPER::FETCHSIZE(@_); } $INC{"My/TiedArray.pm"} = 1; } use strict; use warnings; use feature qw( say ); use My::TiedArray qw( ); tie my @a, My::TiedArray::, qw( a b c ); say "Before loop"; for (@a) { say "In Loop"; } say ""; say "Before loop"; for ((), @a) { say "In Loop"; }
    Before loop FETCHSIZE In Loop FETCHSIZE In Loop FETCHSIZE In Loop FETCHSIZE Before loop FETCHSIZE In Loop In Loop In Loop

      But isn't the fact that FETCHSIZE is called "more than once" (a.k.a. once per iteration) is how we achieve "don't have to calculate the length of the list in advance?". The first call to FETCHSIZE, we fetch from the iterator. If there is no item, we return 0, ending the for(). If there is an item, we return 1 and push() the fetched item in a buffer, and for() continue. In the next iteration, FETCHSIZE is called again. We fetch from the iterator. If there is no item, we still return 1, ending the for(). If there is still an item, we return 1+1=2 and push() the item in a buffer.

      FETCH will shift() item from buffer.

      Where do we calculate the length of the list in advance?

      Range::ArrayIter is a proof of concept of range iterator using tied array. You can use range_iter(1, Inf), for example. Yes, it's slower than object-based and coderef-based iterator.

        No, the approach you describe fails. It gives incorrect result for any of the following:

        for ($additional, @tied) { } map f(), @tied say "@tied"; ...

        Therefore, the class must provide a FETCHSIZE must return the correct size up front. It doesn't matter that you can rely on a bug to get away with it in for (@tied).

        Updated for clarity.