http://www.perlmonks.org?node_id=883036


in reply to Re^5: reduce like iterators
in thread reduce like iterators

I don't know about "magic", but sure, I understand it's not a solution to the semi-predicate problem, but all the solutions are one kind of workaround or another, aren't they? Wouldn't a $^PRE built-in be undef at the start of your grep{} block and cause you the same problem? When it comes to the initial element of the list, wouldn't you still have the same problem no matter what kind of built-in variables you had available to you?

reduce really doesn't make sense because that performs a function on the results of the previous run of the function. $a accumulates...something, but you end up with one value, not a new list. What you want is more like a grep -- deciding whether to allow a current value through based on a previous value.

You can write a function that takes tuples and special cases $a, so it won't be skipped, but you didn't think that was elegant.

You can write a function that initializes $p (or whatever you call it), and hide the details in the function, but you didn't like that either.

In the end, you have to initialize $p, special-case the first element, or write much more complicated code just to avoid initializing $p. It all amounts to the same thing, so why not keep it simple? Obviously the semi-predicate problem is a very real concern, but it's also pretty trivial...

My two cents...

--marmot