Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re^6: reduce like iterators

by furry_marmot (Pilgrim)
on Jan 19, 2011 at 01:59 UTC ( #883036=note: print w/replies, xml ) Need Help??

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...


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://883036]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2018-05-24 06:02 GMT
Find Nodes?
    Voting Booth?