in reply to Re: Fold a list using map splices
in thread Fold a list using map splices

I like the straightforward,

push @result, func1([ splice @_, 0, $num_elements ]) while @_;
but this expression could not be placed in a sub's parameter list because push does not return a list, rather it returns the number of new elements in the list.

To clarify, this technique is used primarily to transform function parameters before they get passed. I could not use your two step method for that case. This technique is useful for creating HTML tables with N columns, where Tr() and td() both accept array refs. And, in order to fold the list without breaking stride, I used map.

As a side note, I have been programming in Lisp for the last couple months, so I'll admit that this technique appears more functional than procedural. :-)

What time is it? It's Camel Time!

P.S. A matter of opinion:
two steps is right? Just because our styles are different does not make either one of them right or wrong. It's a bit strong to say that your code is right, because that implies that my code is wrong. Let's agree that it is *your opinion* that "two steps" is the right/better way or *you prefer* "two steps." As a highly respected individual in this community, you affect the perceived usefulness of every node on which you comment. And I, for one, don't want to believe that you are an arrogant person, so please update your node to reflect that it *is* your opinion rather than a matter of fact.

Replies are listed 'Best First'.
Re^3: Fold a list using map splices
by Aristotle (Chancellor) on Feb 20, 2003 at 17:54 UTC
    I'm not saying that my approach is the sole possible correct one, but I do believe that yours is wrong. The way it turns out to work is more of an accidental feature than something you should rely on. If you really don't want to create a temporary array, I suggest you do something like
    map func1([ splice @_, 0, $num_elements ]), 0 .. (@_/$num_elements);
    Iterating the map over an array while modifying it at the same time is wrong.

    Makeshifts last the longest.

      Ok. After more experimentation, I understand your point. Basically the question you are asking me is "why should I iterate over the last part of the list that may not exist?"

      So, what I need to do is change the list at the end of the map, to a list that will be used to count the new elements, rather than counting all of the elements in the original list.

      While I still disagree with you about "right" and "wrong," you have made a valuable point which makes the map more efficient. Thank you.

      What time is it? It's Camel Time!