Re: Are .key and .value right for Pairs?

2001-10-06 Thread Randal L. Schwartz

 Damian == Damian Conway [EMAIL PROTECTED] writes:

Too much typing:

Damian module PAIR;

Damian method car { return .key }
Damian method cdr { return .value }

Damian method AUTOVIVIFY (default, $name) {
Damian if ($name =~ m/^c([ad])([ad]*)r$/) {

Replace:

Damian my $next = c$2r;
Damian given ($1) {
Damian when 'a': { return sub { .car.$next() } }
Damian when 'd': { return sub { .cdr.$next() } }
Damian }

with:

my $this = c$1r;
my $next = c$2r;
return sub { { .$this().$next() } };

Damian }
Damian }


Right?  Plus or minus a set of parens or something, eh?

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
[EMAIL PROTECTED] URL:http://www.stonehenge.com/merlyn/
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



Are .key and .value right for Pairs?

2001-10-05 Thread Sam Tregar

Can I get a .car and a .cdr please?  In my limited mind key and value
are specific to hashes and their wimpy brother associative lists.

-sam
Can I get a what what?




Re: Are .key and .value right for Pairs?

2001-10-05 Thread Damian Conway

   
Can I get a .car and a .cdr please?  In my limited mind key and value
are specific to hashes and their wimpy brother associative lists.

Sure. Roll-you-own with:

module PAIR;

method car { return .key }
method cdr { return .value }


or, if you're really serious about it:


module PAIR;

method car { return .key }
method cdr { return .value }

method AUTOVIVIFY (default, $name) {
if ($name =~ m/^c([ad])([ad]*)r$/) {
my $next = c$2r;
given ($1) {
when 'a': { return sub { .car.$next() } }
when 'd': { return sub { .cdr.$next() } }
}
}
}


Damian