Created
May 30, 2021 11:07
-
-
Save heyajulia/7bb6f451edfb10f7d94d2338d989c3eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Usage: echo words | raku scrambler.raku | |
sub scramble-word(Str $word --> Str) { | |
if $word.chars <= 3 { | |
return $word; | |
} | |
my @letters = $word.comb; | |
my $first-letter = @letters[0]; | |
my $last-letter = @letters[*-1]; | |
my @middle-letters = @letters[1..*-2]; | |
my @scrambled-word = @middle-letters.pick(@middle-letters); | |
@scrambled-word.prepend: $first-letter; | |
@scrambled-word.push: $last-letter; | |
return @scrambled-word.join; | |
} | |
slurp() | |
.words | |
.map(-> $word { scramble-word $word }) | |
.join(" ") | |
.print; |
The above can be simplyfied to:
sub scramble-word(Str $word) {
(.[0], .[1..*-2].pick(*), .[*-1])[*;*].join with $word.comb
}
However, it can not be simply explained. :)
[*;*]
flattens the list. It can be replaced by .flat
. But why does it work that way?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is my take and yes, I used to code in Perl. :)