Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Re: Re: Are strings lists of characters?

by Ovid (Cardinal)
on Oct 17, 2002 at 20:48 UTC ( [id://206142]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re: Re: Re: Re: Are strings lists of characters?
by Chmrr (Vicar) on Oct 18, 2002 at 05:43 UTC

    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'

Re: Re: Re: Re: Are strings lists of characters?
by John M. Dlugosz (Monsignor) on Oct 18, 2002 at 14:34 UTC
    I think that will be a must-read. Thanks for pointing it out!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-20 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found