in reply to Re: Are strings lists of characters?
in thread Are strings lists of characters?

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.

  • Comment on Re: Re: Are strings lists of characters?

Replies are listed 'Best First'.
Re: Re: Re: Are strings lists of characters?
by Ovid (Cardinal) on Oct 17, 2002 at 20:48 UTC

    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!
(jeffa) 3Re: Are strings lists of characters?
by jeffa (Bishop) on Oct 17, 2002 at 20:47 UTC
    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!

        No. With an iterator, you can generate one element in the list at a time. What happens if your nifty little list happens to contain 50,000 elements that are computationally difficult to produce? Creating them all at once may very well take up too much memory on your system. Yet, if you still need to process them, an iterator allows you to generate the items one at a time without eating all of your ram and swap space.

        It's possible, though, that I am simply considering a different problem than what you are intending. If you post cost snippets of what you wish was in Perl, perhaps that might clear up some confusion.

        Cheers,
        Ovid

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

Re: Re: Re: Are strings lists of characters?
by shotgunefx (Parson) on Oct 17, 2002 at 23:01 UTC
    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."