Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^6: reduce like iterators

by furry_marmot (Pilgrim)
on Jan 19, 2011 at 01:59 UTC ( [id://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...

--marmot

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://883036]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-26 03:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found