Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^6: Getting for() to accept a tied array in one statement

by perlancar (Hermit)
on Apr 16, 2019 at 14:23 UTC ( [id://1232665] : note . print w/replies, xml ) Need Help??


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

hdb, you're my hero :-) My faith in Perl is restored again.

Replies are listed 'Best First'.
Re^7: Getting for() to accept a tied array in one statement
by ikegami (Patriarch) on Apr 19, 2019 at 08:45 UTC

    Why do you want to make something so simple so complicated and expensive? Use an iterator!

    sub make_list_iterator { my @list = @_; return sub { return () if !@list; return shift(@list); }; } my $iter = make_list_iter("some", "el", "ems"); while ( my ($item) = $iter->() ) { ... }

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

      I believe (or at least this is how I understand it) that the point of the exercise is SoC. The 'wrapper' causes a separate function to execute every time an element inside of the array is accessed and this function is defined somewhere else (not inside the while loop). Your code could work in case it would have used a reference array instead. But I think you mean with 'expensive' you mean using the tie?

      Anyways, if you would use an array reference your code does work:

      use lib '.' ; use strict ; use warnings ; use ar ; my @ar ; my $ar2 = tie @ar, "ar" ; @ar = (1, 2, 3) ; sub make_list_iterator { my $list = $_[0] ; return sub { return () if !@{$list}; return shift(@{$list}); }; } my $iter = make_list_iterator(\@ar) ; while ( my ($item) = $iter->() ) { print "$item\n" ; }

      I'm not showing how to implement ar, plenty of examples in previous posts

        Why????? The code I posted already worked as-is. No need to create a tied array and the underlying class. My code already calls a sub for each element.

      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.

        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