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.
  • Nice work, but still something lacking in the "show" function: both your version and SF's have too much repetition. What we really want to to say that there's a list of three actions to perform in one of three orders, with only one of the blocks moving. something like:

    sub show(%tree, Traversal $method) {
        return unless %tree;
        my @actions = (
            { show %tree<RIGHT>, $method },
            { show %tree<LEFT>, $method },
        );
        @actions.=splice( $method.value, 0 ) <== { take %tree<VALUE> };
        @actions>>.();
    }

    Perhaps losing a bit of readability ... but I think we can remove even the last bit of repetition here:

    sub show(%tree, Traversal $method) {
        return unless %tree;
        my @actions = <RIGHT LEFT>.map: -> $way { show %tree<$way>, $method }.assuming( $^way );
        @actions.=splice( $method.value, 0 ) <== { take %tree<VALUE> };
        @actions>>.();
    }
    • It's clearly a matter of taste of where you want to end up on the readability/non-repetitiousness scale. :) I simply chose to err on the side of readability this time around.

      • And actually I made a horrible error in my code: the way I wrote it the traversal order is actually undefined, because the order of iteration of hyper operators is undefined. So, although I'd say that having a concurrent traversal ordering would be a nice addition, the last line of my sub should probably be something like: ".() for @actions;"
    • Am I the only one whose OCD goes on overdrive everytime he sees a fixed-function tree traversal sub?

      I immediately want to refactor &show into a general traversal sub:

      sub traverse(%tree, Traversal $order, &function) { ... &function(%tree<VALUE>); ... }

      and a trivial driver:

      sub show(%tree, Traversal $order) { traverse(%tree, $order, &take) }

      I guess that gets away from keeping the same overall form as the E02 code. Of course, I had the same reaction the first time I

      • Heh. The reaction I got on #perl6 was that I had changed the function too much by replacing print by take, essentially turning the &show function into an iterator generator.