in reply to map: chaining vs. nesting

1. Nothing specific outside of a general recognition that some copies can be avoided. A map's result is pushed onto the stack (assuming list context) for the subsequent op to do something with it. It is a copy but it is marked as something that can be recycled. Stuff that recognizes that (like map itself) can avoid making extra copies. I think  = map( EXPR, map( EXPR, LIST ) ) copies the values from LIST once and then as possible, just re-uses the copies for the next map which gives it to the aassign which also notes that the input is recycleable and steals it instead of making another copy.

2. Not terribly useful. I used chained maps where the output from one is not the same size as the input. The subsequent maps deal with these entirely different lists. I suppose it could be moved into the map but that just makes the code harder to read.

3. See #1. Something of a fashion, in a more general sense already happens.

Replies are listed 'Best First'.
Re^2: map: chaining vs. nesting
by Roy Johnson (Monsignor) on Jun 18, 2004 at 15:55 UTC
    Regarding #2: The fact that map output size differs from input size doesn't mean there's no benefit to nesting vs. chaining. The benefit I expected was in memory overhead (from not having so many copies resident at once) more than in reduction of operations (from copying multiple times). I agree with your point about nesting being harder to read; that's why I suggested it as an optimization.

    Putting the idea in better terms, I'm suggesting that map, in an ideal world where perl development was effortless, should behave like (1..100000): when its output is assigned, the range generates the whole list; but when its output is used iteratively (at least, in a foreach loop), it simply generates one output value at a time, as needed. map, grep, and ranges could all behave that way when feeding map, grep, or foreach.


    We're not really tightening our belts, it just feels that way because we're getting fatter.
      Lazy generation is a feature you can specify in perl6. Not yet so for perl5.