Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by ikegami (Patriarch)
on Apr 22, 2019 at 21:56 UTC ( [id://1232883]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^10: Getting for() to accept a tied array in one statement
by perlancar (Hermit) on Apr 23, 2019 at 04:38 UTC

    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.

        Which statement was your "no" responding to? The original statement I believe is: "With for(@tied_array) we also doesn't need to calculate the length of the list in advance" which you said "That's not true." which I then demonstrated with Range::ArrayIter, which then you replied to with "no" followed by cases where tied-array-based iterator would fail. Sure, the tied-based iterator does not work in all cases, but we are talking specifically about a single "for(@tied_array)".

        What I want to know is why you said (in essence) that "With for(@tied_array) we still need to calculate the length of the list in advance".

        Perhaps we need to consult p5p before deeming it as a bug :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1232883]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-03-28 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found