in reply to Re: Fold a list using map splices
in thread Fold a list using map splices
The index variables there kept tickling me as not right. This is Perl, not C, there has to be a way to do this without explicit indices and no copying. Guess what. :) It's actually simpler than I was expecting and definitely goes in my "cool snippets" file.
sub mapn (&@) { my ($cb, $n) = splice @_, 0, 2; splice @_, 0, 0, (undef)x$n; map{ splice @_, 0, $n; $cb->(@_[0 .. (@_ < $n ? $#_ : $n-1)]); } 0 .. (@_/$n)-1; } sub mapnz (&@) { my ($cb, $n) = splice @_, 0, 2; splice @_, 0, 0, (undef)x$n; map{ splice @_, 0, $n; $cb->(@_[0 .. $n-1]); } 0 .. (@_/$n)-1; }
BrowserUk++ for the inspiration!
Update: fixed mapnz
Makeshifts last the longest.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re^2: Fold a list using map splices
by BrowserUk (Patriarch) on Feb 22, 2003 at 06:31 UTC | |
by Aristotle (Chancellor) on Feb 22, 2003 at 15:24 UTC | |
by hypochrismutreefuzz (Scribe) on Sep 04, 2004 at 01:38 UTC | |
by Aristotle (Chancellor) on Sep 04, 2004 at 03:01 UTC | |
by hypochrismutreefuzz (Scribe) on Feb 20, 2006 at 14:02 UTC | |
| |
by BrowserUk (Patriarch) on Feb 22, 2003 at 21:09 UTC |
In Section
Snippets Section