Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^3: reduce like iterators

by ikegami (Patriarch)
on Jan 03, 2011 at 19:54 UTC ( [id://880252]=note: print w/replies, xml ) Need Help??


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

Your reduce_list is named similarly to reduce, but bears no resemblance since it doesn't to reduce at all.

  • reduce allows an arbitrary state to be passed from one pass to another. Your reduce_list doesn't.

  • reduce can return any value, not just the input. reduce_list can return at most one scalar, and it can only be the input. (That's not very "listy"!)

Because of those reasons, reduce is a general purpose function. (It can implement any other function in List::Util.) Your reduce_list is just grep with access to the last element.

This accounts for the differences with what I suggested it should look like.

Replies are listed 'Best First'.
Re^4: reduce like iterators
by LanX (Saint) on Jan 03, 2011 at 22:08 UTC
    As already stated in the OP the need to appropiately name a whole new family of reduce-like iterators is an extra problem.

    At least for me it's more complicated than implementing them.

    But if it's easier for you to baptize reduce-like maps, greps, parts, and so on in a quick post, I would appreciate to hear your suggestions

    If I was forced to chose only one implementation I would certainly chose a reduce like map, like you did, because it has the biggest flexibility.

    But I'd prefer to have a solution where map is still named map and grep ist still grep!

    IMHO extra options and special vars should activate and supply the needed functionality.

    Cheers Rolf

      As already stated in the OP the need to appropiately name a whole new family of reduce-like iterators is an extra problem.

      Your always free to not give it a name, leaving it as

      my @b; for (@a) { push @b, $_ if !@b || $_ ne $b[-1]; }
      or
      my @b = list_reduce { 0, $a || $b ne $_->[-1] ? $b : () } 1, @a;

      But they're not very readable.

      But I'd prefer to have a solution where map is still named map and grep ist still grep

      You complain that there's "a lack of iterators allowing to compare successive elements", yet you wouldn't want to use them if they existed?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2024-04-25 01:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found