Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Perl 6 implementation of the following Perl 5 code

by moritz (Cardinal)
on May 27, 2013 at 11:13 UTC ( [id://1035391]=note: print w/replies, xml ) Need Help??


in reply to Perl 6 implementation of the following Perl 5 code

A solution similar to yours in style:

use v6; print join ' ', reverse .words for lines.grep(/SRC1/)\ .map({ [+<<.words[*-1, 0], $_]})\ .sort\ .map(*[2])

And a version using feeds (note that feeds support in Rakudo is still somewhat lacking and experimental, but this one works:)

use v6; lines() ==> grep(/SRC1/) ==> map({ [+<<.words[*-1, 0], $_]}) ==> sort() ==> map(*[2]) ==> my @lines; print ~.words.reverse for @lines;

And here is another solution:

class Line { has $.str handles 'words'; method Str { self.words.reverse.join(' ') } } multi infix:<cmp>(Line $a, Line $b) { $a.words[*-1] <=> $b.words[*-1] || $a.words[0] <=> $b.words[0]; } print lines().map({Line.new(str => $_)}).grep(/SRC1/).sort;

(Which admittedly doesn't do the Schwartz Transform).

(Update: added second version, tested and fixed third version)

Replies are listed 'Best First'.
Re^2: Perl 6 implementation of the following Perl 5 code
by konnjuta (Acolyte) on May 28, 2013 at 00:34 UTC

    The 1st/2nd Perl 6 solution is interesting because you read it from left to right treating map,grep and sort as object methods.

    The Perl 5 version is read from right to left treating map,grep and sort as functions. I've attempted to do it the functional way in Perl 6 and got the following to work on Rakudo.

    use v6; say reverse .words for map {.[1]} ,sort( map { [.words[*-1],$_] } ,grep {/SRC1/} ,lines() );

    The syntax for doing Perl 6 functionally seems to be more untidy than the Perl 5 version and make it a bit harder to read. The required comma, the required parenthesis around sort. Its not too bad though. Will get some getting use to.

      If you make the commas stand out by putting them up front, it looks untidy, yes. As they say, the key is to play to Perl's strengths, not to its weaknesses.

      And if you include unnecessary punctuation, it also looks untidy. This one seems to work fine:

      use v6; say reverse .words for map *[1], sort map { [.words[*-1],$_] }, grep /SRC1/, lines;

        I can confirm this works and Infact its actually much cleaner for people who likes one-liner

        say reverse .words for map *[1], sort map {[.words[*-1],$_]}, grep /ICSE/, lines;

        Hmm ... now I'm wondering why I couldn't get the sort to work without the parenthesis previously.

Re^2: Perl 6 implementation of the following Perl 5 code
by Jenda (Abbot) on May 27, 2013 at 12:27 UTC

    Waitasec?!? What's the backslash at the end of the lines in the first solution? Looks suspiciously like a line continuation character in the worst tradition of Visual Basic and Ruby?!?

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      It is "unspace". Whitespace is not allowed before subscripts or ".". Apparently this is required to allow "if" conditions without parens and is part of the "space is sometimes significant" path that was taken. (See: Synopsis 3 and the linked Synopsis 2)

      Update: Below, moritz clarifies that "if" without parens was not the reason for forbidding whitespace around "." methods calls, merely another of the consequences.

      Good Day,
          Dean

        Way too late for the discussion, but one wonders if the removal of if condition parens is really worth the requirement to invent a concept such as "unspace".

        I mean really? "unspace"! If ever there was a 'seemed like a good idea at the time' that became 'a rod for our own backs' in the making; that's gotta be it.

        Especially, if most potential users are like me, and will always be using a mix of different languages at the same time.

        I'm pretty sure that if I ever make any regular use of P6, I will use parens around if conditions anyway. Simply because it is easier to not go through the pain of unlearning the habit (perhaps:'unparensing'?) for just one of the languages I (potentially) use every day.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-18 00:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found