[Purdue-pm] I like Raku's operators

Mark Senn mark at purdue.edu
Tue Jun 22 05:11:29 PDT 2021


I read some of the answers (but not the one that uses this method)
to the Perl (and Raku---formerly known as Perl 6) Weekly Challenge problem
and thought I could write one that is easier to understand.  Here it is:



#!/home/pier/e/mark/sw/ubuntu-18.04-x86_64/rakudo-star-2021.02.1/bin/raku

# From https://perlweeklychallenge.org/blog/perl-weekly-challenge-117/#TASK1
#     You are given text file with rows numbered 1-15 in random order
#     but there is a catch one row in [sic -mark] missing in the file.
#
#     11, Line Eleven
#     1, Line one
#     9, Line Nine
#     13, Line Thirteen
#     2, Line two
#     6, Line Six
#     8, Line Eight
#     10, Line Ten
#     7, Line Seven
#     4, Line Four
#     14, Line Fourteen
#     3, Line three
#     15, Line Fifteen
#     5, Line Five
#
#     Write a script to find the missing row number.

# The lines from the text file.
my $heredoc = qq:to/END/;
11, Line Eleven
1, Line one
9, Line Nine
13, Line Thirteen
2, Line two
6, Line Six
8, Line Eight
10, Line Ten
7, Line Seven
4, Line Four
14, Line Fourteen
3, Line three
15, Line Fifteen
5, Line Five
END

# Inside every large program is a small program struggling to get out.
# ---Tony Hoare, famous computer scientist

# This task can be done with 65 characters:
(([+] (1..15)) - ([+] (comb /\d+/, $heredoc).map({.UInt}))).say;

# Below is a better explanation.

# Sum 1, 2, ..., 15.
# "[+] (1..15)" is the the same as 1 + 2 + ... + 15.
my $allsum = [+] (1..15);

# Sum the heredoc numbers.
# "comb /\d+/, $heredoc" matches all strings of one
# or more digits and returns a list---comb matches
# what I'm looking for---split matches what I'm not
# looking for---I use comb much more often.
# The "map({.UInt})" converts each string to a number.
my $heresum = [+] (comb /\d+/, $heredoc).map({.UInt});

# Print the answer.
($allsum - $heresum).say


More information about the Purdue-pm mailing list