in reply to Problem emulating built-ins like map and grep.
Update: I can't believe I didn't think about the list being returned. However, the while loop still avoids building a 1 .. @_ / 2 list, thus still saving effort.#!/usr/bin/perl -w use strict; sub map2 (&@) { my $code = shift; if(@_ % 2) { require Carp; Carp::croak('Odd number of values in list'); } local ($a, $b); my @r; push @r, $code->() while ($a, $b) = splice @_, 0, 2; @r; } my %hash = qw/A B C D E F G H/; # let's remove the multiple calls to print() and # make the use of a map justifiable, shall we? print map2 sub { "key $a => value $b\n" }, %hash;
Makeshifts last the longest.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Problem emulating built-ins like map and grep.
by BrowserUk (Patriarch) on Jan 22, 2003 at 22:50 UTC | |
by Aristotle (Chancellor) on Jan 22, 2003 at 23:39 UTC | |
by BrowserUk (Patriarch) on Jan 23, 2003 at 00:28 UTC | |
by Aristotle (Chancellor) on Jan 23, 2003 at 07:40 UTC |