in reply to Re: Golf Question - Shortest way to write while inside while
in thread Golf Question - Shortest way to write while inside while

Bugs:

Golf

sub w(&$){&{$_[1]}while&{$_[0]}}sub d(&){$_[0]} # then, instead of while (foo) { bar }, w{foo}d{bar};
sub l(&){{&{$_[0]};redo}}sub w{$_[0]||last} # then, instead of while (foo) { bar }, l{w foo;bar};

Replies are listed 'Best First'.
Re^3: Golf Question - Shortest way to write while inside while
by ambrus (Abbot) on Nov 08, 2007 at 10:16 UTC
    • The proto is &, not \&.

    True. I keep messing this one up.

    • x->() is shorter than &{x}().
    • y while x is shorter than while (x) { y }.
    • || is shorter than or .
    • The label is not needed in the second snippet.

    Yep. I thought of the case when you need lots of while loops, so the size of the definition doesn't really matter, only the way you use them.

    • {x;redo} is shorter than x while 1.

    Doesn't help here anymore I guess, because we have nonalnums around while.

    • &{x} is even shorter if you don't care about @_.

    Yes, but that doesn't really help because you don't get the @_ of the loop context, only the @_ passed to w or l.

    While we're there, &{+pop} is even shorter than &{$_[0]} in the defn of l, and pop is shorter than $_[0] in w.

    • Proto not needed for w in second snippet.

    It puts the condition in scalar context, like while does, so it might save some characters in the loops.