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

Re: Look-ahead arguments in for loops

Thread Previous | Thread Next
From:
John Macdonald
Date:
October 1, 2005 19:13
Subject:
Re: Look-ahead arguments in for loops
Message ID:
20051002024204.GA14677@lupus.perlwolf.com
On Sat, Oct 01, 2005 at 02:22:01PM -0600, Luke Palmer wrote:
> And the more general form was:
> 
>     $sum = reduce { $^a + $^b } @items;
> 
> Yes, it is called reduce, because "foldl" is a miserable name.

So, the target of running a loop with both the current
and previous elements accessible could be written as either:

    reduce :identity undef
	{ code using $^prev and $^cur ... ; $^cur }
	@items;

or:

    reduce :identity @items[0]
	{ code using $^prev and $^cur ... ; $^cur }
	@items[1...];

-- 

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