in reply to Are strings lists of characters?

If you really want a lazy list, could you just use an iterator? The following will return the individual characters, but only as you need them. Further, it won't do a split, but it does reverse the string internally, so a very long string may be an issue. I just hacked it together to demonstrate one strategy. It could use some clean up.

#!/usr/bin/perl -w use strict; sub NEXT { $_[0]->() } sub string_to_char_iter { my $string = shift; $string = reverse $string; sub { '' ne $string ? chop $string : undef } } my $string = join '', 'a' .. 'z'; my $iter = string_to_char_iter $string; while ( defined ( my $char = NEXT $iter ) ) { print "$char\n"; }

Cheers,
Ovid

Update: Just in case it's not clear, this was just some demo code. Obviously, for a string of 26 characters, creating an iterator would be overkill. Iterators are going to be more useful if you have a large amount of data that is difficult to fit into memory, such as reading from a file, or if you need to keep track of where you are in your data while reading it.

Oh, and I tweaked the code just a hair.

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
Re: Re: Are strings lists of characters?
by John M. Dlugosz (Monsignor) on Oct 17, 2002 at 20:11 UTC
    That's fine for a while loop, but map wouldn't know what to do with it. That's why we need the iterator at the language level. I suppose the implementation of the iterator would not be any different, rather Perl 6 "knows" that the iterator is in fact an iterator and will use it transparently.

      Yes, but you can write your own version of map that takes a code reference as the first argument and an iterator as the second argument, thus solving your problem for Perl 5, rather than having to wait for Perl 6 to come out during Christmas :)

      For more information on this, you can go to http://perl.plover.com/book/, subscribe to the mailing list and read the sample chapter. While I don't think that Dominus would mind my posting a brief code snippet to illustrate, I'm not entirely certain if that's appropriate, because he has asked that the chapter not be distributed (or even saved). As a result, I'm not entirely certain if it would be appropriate to post the code.

      However, if you check it out, search for the &imap function. It seems to resolve what you're looking for. Again, I'd post it myself, but I'm not sure of what's appropriate there.

      Update: I contacted Dominus via email to inquire about the appropriateness of this and he replied that his only reason for wanting to prevent distribution is to revise and correct the chapter so as to avoid error-filled drafts floating around the 'Net. Posting a snippet is therefore okay.

      #!/usr/bin/perl -w use strict; sub NEXT { $_[0]->() } sub imap (&$) { my ($transform, $it) = @_; return sub { my $next = NEXT($it); return unless defined $next; return $transform->($next); } } sub string_to_char_iter { my $string = shift; $string = reverse $string; sub { '' ne $string ? chop $string : undef } } my $string = join '', 'a' .. 'z'; my $iter = string_to_char_iter $string; my $uc_chars = imap { uc $_[0] } $iter; while ( my $char = NEXT $uc_chars ) { print "$char\n"; }

      For that code, we pass in a subref and an iterator (which is also a sub ref. We return yet another sub reference that will apply the first subref to the value returned from the iterator. In otherwords, we use the imap() function to transform one iterator into another, getting the results that you may need.

      Cheers,
      Ovid

      Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

        I have not read Dominus' chapter, so I spent a minute pondering and came up with the following definition of imap, which is probably much less elegant than his:

        sub imap(&$) {defined($_=$_[1]->())?($_[0]->($_),&imap(@_)):()}

        Can you tell I've been writing too much Scheme recently? ;> I also was pondering other implementations of the example iterator discussed earlier, and thought that

        sub sub_iter { my ($s) = @_; return sub {$s?substr $s, 0, 1, '':undef} }

        ..might be a hair faster than the reverse. Benchmark says it's not, though; oh, well.

        perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

        I think that will be a must-read. Thanks for pointing it out!
      How about wrapping Ovid's while loop in another subroutine:
      use strict; sub NEXT { $_[0]->() } sub string_to_char_iter { my $string = shift; $string = reverse $string; sub { chop $string } } sub get_all { my $iter = shift; my (@list,$char); push @list,$char while $char = NEXT $iter; return @list; } my $string = join '', 'a' .. 'z'; my $iter = string_to_char_iter $string; print $_,$/ for map uc, get_all($iter);
      UPDATE: changed while loop to one liner to irk the Java types >:)

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
        But iterating over the whole thing first defeats the purpose of having a lazy list!
      While I personally miss the built in support of iterators. (I was working on a patch to Want.pm for Iterator context, but haven't had the time to finish), you can get the DWIM with iterator closures.
      #!/usr/bin/perl use warnings; use strict; sub char_iterator(\$){ my $str = shift; my $count = 0; return sub { if (wantarray){ my ($tc,$len) = ($count, (length($$str) - $count) ); $count = length($$str); return split('', substr($$str,$tc,$len)); }else{ return substr($$str,$count++,1); } } } my $string = join('', ('a'..'z') x 3); my $chariter = char_iterator($string); # Get one char at a time. while(my $char = $chariter->() ){ print "Got $char\n"; } # Get it in list context. my $mapiter = char_iterator($string); my @upper = map { uc($_)."\n" } $mapiter->(); print @upper;

      update I had a possible workaround to the "lazy evaluate" situation here. I still plan to explore this further as time permits. I'd be interested to hear your thoughts.

      -Lee

      "To be civilized is to deny one's nature."