Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • If you asume that any block got FIRST and LAST but by default those fellows are empty function calls, you could move the condition to call FIRST and/or LAST out of the pointy block and into for.

    The only condition you have left then would be if the list is empty. "for" has to check that anyway (at some point) so you don't really add anything.

    I don't really like to have $LOOP_HAS_RUN = True; any time you call the pointy block.

    I tried to implement it in javascript and ended up with the following.

    window.super_for = function(it, code){
        var first = code.FIRST ? code.FIRST : function(){};
        var last = code.LAST ? code.LAST : function(){};

        var current = it();
        if (current !== iterable.done) {
        first();
        } else {
        return;
        }

        do {
        code(current);
        } while( (current = it()) !== iterable.done );

        last();
    };

    In perl6 I would try (if I would know how :) to transform:

    for @list -> $a {
      FIRST { say "i haz a first!" }
      say "$a";
      LAST { say "i is done" }
    }

    into:

    fancy_for(@list, $code, $code_first, $code_last);

    You could default $code_first and $code_last to an empty block and let the optimizer remove those.
    • Your solution works fine when the FIRST and LAST does not access any lexical variables specific to the loop block. $a in your Perl 6 code at the end is such a variable. Sending $code_first and $code_last in as unrelated closures doesn't give those closures access to $a.

      For a solution that does work for these cases, please re-read the blog post. :)