On Thu, Aug 03, 2000 at 06:31:38AM +0000, Martyn J. Pearce wrote:
> And I feel that by being able to iterate over lists with map, grep,
> fold[rl]/reduce{,_r}, whatever, _without pre-flattening the list_, we
> could drastically increase the applicability of these constructs.

Having iterators avaliable in the language and being able to say that
a sub takes an iterator could be a great advantage. Also being able
to create iterators.

ie being able say

sub reduce (&+) { ...}

where + is an iterator and have an iterator context so

  reduce { ... } some_func()

and have some_func know it is being called in an iterator context
and be able to create it's own iterator. foldr could then be
done as

  reduce { ... } reverse some_func()

which could create a reverse iterator.

So there probably should be an RFC to add iterators to the language and
be avaliable from perl, not just C. But I have no idea how they
should look etc.

However, and I am makeing an assumption here, probably the most common
use of reduce is with one of the + - / * operators. So it would be nice
to be able to optimize these, maybe to a separate op. There is two ways
we can do that.

1) Allow a syntax for it

    reduce (+) @list;

2) Detect and optimize like we do now for sort.

  reduce { $a + $b } @list;

But to be able to do either require reduce to be in the perl core
rather than a module as it requires either the lexer or optimizer
to know about reduce.

Graham.

Reply via email to