Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Method Calls on multiple objects

by dragonchild (Archbishop)
on Mar 27, 2004 at 01:59 UTC ( [id://340193]=note: print w/replies, xml ) Need Help??


in reply to Method Calls on multiple objects

I'm a little confused ... What benefit does this provide? As far as I can tell, this is basically a rewriting (and complicating) of:
$_->$method(@args) for @objects;

Personally, I don't see a need for calling the same method on a number of objects. If you need an aggregate, create an aggregate. This, to me, would be a red flag in a code review.

------
We are the carpenters and bricklayers of the Information Age.

Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Replies are listed 'Best First'.
Re: Re: Method Calls on multiple objects
by Fletch (Bishop) on Mar 27, 2004 at 02:20 UTC

    Or even capturing return values:

    my @rets = map { $_->$method( @args ) } @objs;

    Or allowing returns of lists:

    my @rets = map { [ $_->$method( @args ) ] } @objs;
      So, why would an aggregate be better? What syntactic sugar are you looking for?

      ------
      We are the carpenters and bricklayers of the Information Age.

      Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

        What syntactic sugar are you looking for?

        He was arguing (as I was) against your post that there never comes a need to call a method against a collection of objects. It was *you* who were were saying the demand for an aggregate was a red-flag in a code-review, and then you complain our percieved advocacy of aggregates, neither of which we have posted. Both fletch and I have mentioned foreach, map, grep, and so on -- not aggregates. Please reread our posts, as I think you are confusing our posts with those of Limbic~Region, who created a very special-case agg. (not just 'RandomSpaceTravelers' (which is a fair collection class if you really need one) but a much different (and bizarre) function calling interface for a list of random space travellers). We aren't the same people, and we aren't making the same points.

Re: Re: Method Calls on multiple objects
by flyingmoose (Priest) on Mar 27, 2004 at 02:18 UTC
    Personally, I don't see a need for calling the same method on a number of objects. If you need an aggregate, create an aggregate. This, to me, would be a red flag in a code review.

    # make 8 random aliens @lifeforms = map { RandomSpaceTraveller->new(); } (0..8); # find out who is klingon @klingons = grep { $_->get_species() eq 'klingon' } @lifeforms; # blow the klingons away foreach (@klingons) { $kirk->shoot_at($_); };

    As evidenced above, I disagree. There are those of the functional school (of which I am quickly becoming a convert), who would say mixing OO and functional concepts in the same program is extremely funky in a good way.

    Another point -- What if you are implementing an aggregate? Well, you need to know how to dance in aggregate school.

      I'd agree with dragonchild. Yes, combining those styles is good. But strive to do it naturally, and you won't likely find the proposed method useful in many situations.

      Remember, many small utilities that fit together in flexible ways. That's the ticket. :-)

        But strive to do it naturally, and you won't likely find the proposed method useful in many situations.

        I am not psychic here, what does 'more naturally' mean to you? Please elaborate and we can debate opinions on an equal front, but one saying 'what you do is not natural' is a assault on code without backing -- please explain your convictions and the merits of one style over another, and what is 'more or less natural' to you. You may find there are many of those who disagree, and as it stands, we don't even know what you mean. It's sort of like me saying "I am flyingmoose and your code is wrong because I am flyingmoose". That doesn't help folks, really it doesn't, and perhaps you need to look more carefully about what you are saying when you say it.

        Also, 'the proposed method' ... mine? Or Limbic~Regions. As I have been maintaining many times, I don't like Limbic's proposed method. I'm talking foreach, grep, and map! Seriously... are you arguing against foreach, grep, and map? Because it appears you have me confused with Limbic (as does dragonchild). Personally I think a simple use of non-combined map's or grep's is very natural, very readable, and obvious. I don't like dragonchild's particular re-coding of my Star Trek example -- I find that very unnatural, inflexible, and quite the maintaince problem.

      So, in your example, the aggregate would be of the RandomSpaceTraveller objects, right? Personally, assuming there's nothing more to your example, I would write that as:
      $kirk->shoot_at( $_ ) for grep { $_->get_species eq 'klingon' } map { RandomSpaceTraveller->new } 0 .. 8;

      If you're implementing an aggregate ... we already have a perfectly good aggregate - it's called an array. If you need a named aggregate, use a hash. How does this additional indirection benefit us?

      ------
      We are the carpenters and bricklayers of the Information Age.

      Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

        Personally, assuming there's nothing more to your example,
        You missed the point. My commentary is there comes a time when it is required to do one thing to many objects (I don't like L~R's suggestion, really, I'm just talking about map and foreach and grep), and I never make examples where there is 'nothing more to them'. These were examples of how they might work in a larger context, and I really don't like being picked apart because I decided to chose a creative example for your amusement.

        Cars and dogs and CD collections are boring, I livened it up a bit. In your example, you do some things I consider to be really odd in the context of a larger app, and it takes away from the meaning of my hypothethical example, perhaps intended to mock it? Do you ever create anonymous data, do things to it, and completely discard it? No. Your example has problems in this regard -- it has hardly any application, even if you were writing a real world application about RandomSpaceTravellers. Your example, as you posted, would raise red-flags in my code review. How are you going to extend that convulted algamation of greps and sorts? What was it you were trying to achieve? Is everything done with side-effects since you don't keep any of the objects around?

        If you're implementing an aggregate ... we already have a perfectly good aggregate - it's called an array. If you need a named aggregate, use a hash. How does this additional indirection benefit us?

        Which was my point exactly when you originally said the following puzzling comment:

        If you need an aggregate, create an aggregate. This, to me, would be a red flag in a code review.

        You didn't state why this would be a red-flag, but I assume you have reasons, and you later went on to say that arrays are all well and good (as I maintain). So if you are for whatever reason making an aggregate (why would you? -- but maybe you have to pass a dragonchild code review where you work and must), yes, you are going to come into a case where you need to iterate over a collection, which is in disagreement with:

        Personally, I don't see a need for calling the same method on a number of objects

        As the many examples show thus far, there are plenty of valid uses ... your particular coding style may dictate otherwise, though yours is not the only style. There are uses, and I originally posted something much more similar to Fletch (which also has uses, even in simple scripts) -- but decided I'd liven it up a bit.

        We probably need to agree to disagree here. We have contrasting opinions, and perhaps I will stick to replying to the original thread posts on PerlMonks before I get accused again of making examples with 'nothing to them'. But again, I was never maintaining we needed a function-multiplexor class, I'm only saying (as most monks will back me up) -- grep and map and foreach are good functions, and calling methods on a list isn't so goofy as you seem to imply.

Re: Re: Method Calls on multiple objects
by Limbic~Region (Chancellor) on Mar 27, 2004 at 14:56 UTC
    dragonchild,
    What benefit does this provide?
    You really have me there. The person asking for this didn't say why they needed/wanted it. The only benefit I can see to my first solution is that it saves you a lot of typing if you are using it all over the place. By sticking all the objects in an array, as you have shown, you get the same shorthand as if you stuck them all in a new object. I will be sure to pass this along.

    Cheers - L~R

Log In?
Username:
Password:

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

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

    No recent polls found