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

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)

Replies are listed 'Best First'.
Re: (jeffa) 3Re: Are strings lists of characters?
by John M. Dlugosz (Monsignor) on Oct 18, 2002 at 14:29 UTC
    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: With an iterator, you can generate one element in the list at a time.

        I agree. But jeffa's function is calling it in a tight loop to build an actual list, then feeding the list to map.