develooper Front page | perl.perl6.language | Postings from October 2001

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

Thread Previous | Thread Next
From:
Damian Conway
Date:
October 5, 2001 17:43
Subject:
Re: Are .key and .value right for Pairs?
Message ID:
200110060043.KAA44021@indy05.csse.monash.edu.au
   
   > 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

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About