Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: 99 Problems in Perl6

by gaal (Parson)
on Dec 15, 2006 at 22:15 UTC ( [id://590131]=note: print w/replies, xml ) Need Help??


in reply to 99 Problems in Perl6

In #8, you have a superfluous assignment of the closure. Instead of

my $compress = do { my $previous; $compress = sub ($x) { ... } };

You could just arrange for the sub definition to be the last evaluated thing inside the do. (This is really no different from Perl 5, except for the formal parameter.)

my $compress = do { my $previous; sub ($x) { ... } }; # or perhaps, my $compress; { my $previous; $compress = sub ($x) { ... } }

Replies are listed 'Best First'.
Re^2: 99 Problems in Perl6
by gaal (Parson) on Dec 15, 2006 at 22:19 UTC
    On second thought, the only reason you have a closure here at all is to preserve state between calls. Use a state variable then, and the more normal function definition syntax.

    sub compress ($x) { state $previous; if $x ne $previous { return $x; } else { return; } }
    Update: I think the if is way too long, but can't remember if there's a context-safe way to return nothing in Perl 6 (that isn't "fail"), to golf out the "return" and say something like return $x ne $previous ?? $x !! $NOTHING_AT_ALL;. In the current application () is probably safe.

      You know, we're getting pretty close to the terseness of Haskell, but (I think) it's still rather readable:

      my $compress = sub ($x) { state $previous; $x ne $previous ?? $previous = $x !! return; }

      And in Haskell:

      compress :: Eq a => [a] -> [a] compress = map head . group

      Update: as concise, but less "golfy":

      my $compress = sub ($x) { state $previous; return $x ne $previous ?? $previous = $x !! (); }

      Cheers,
      Ovid

      New address of my CGI Course.

        Here's a version of the Perl 6 code that's almost as terse as average Haskell code:

        my $cmp = sub ($x) { state $prv; $x ne $prv ?? $prv = $x !! return; }
        Your Haskell compress unfortunately bottoms out on empty input. It's otherwise much better than the stateful Perl 6 version.
Re^2: 99 Problems in Perl6
by Ovid (Cardinal) on Dec 15, 2006 at 22:22 UTC

    Good points, thank you. I suppose if I really want to confuse people (again, pretty darned close to Perl 5):

    my $compress = do { my $previous; sub ($x) { $x ne $previous ?? $previous = $x !! return; }; }

    That works just fine, but it might give someone pause :) Of course, I wouldn't write it like that for production.

    Cheers,
    Ovid

    New address of my CGI Course.

      I thought I read recently that in Perl 6, this:

      ... ?? $previous = $x !! ...;

      would be a syntax error. You need parens (I think Pugs lets you get away with this for now but that will get fixed eventually).

      - tye        

        Indeed, you recall correctly; see 587242.
Re^2: 99 Problems in Perl6
by eric256 (Parson) on Feb 21, 2007 at 18:43 UTC

    Couldn't that be done easier with take/gather?

    my @in = <a a a a b c c a a d e e e e>; my @out = gather { for @in -> $i { state $last; take $i if $i ne $last; $last = $i; }}; say @out;

    ___________
    Eric Hodges
      TIMTOWTDI of course, but syntax updates to gather since the OP allow you to write it as:

      my @in = <a a a a b c c a a d e e e e>; say gather for @in -> $i { state $last; $last = take $i if $i ne $last; };

        Excellent. I'd been looking for a better syntax as i was sure there was one..now if perl6 just had a way to access the last element returned by for you could simplify it even one more time.


        ___________
        Eric Hodges

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://590131]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-24 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found