[Purdue-pm] The Weekly Challenge 147

Mark Senn mark at purdue.edu
Sun Jan 16 19:09:38 PST 2022


This week's weekly challenge is at
    https://theweeklychallenge.org/blog/perl-weekly-challenge-147/

My Raku solutions are at
    https://engineering.purdue.edu/~mark/twc-147-1.pdf
and
    https://engineering.purdue.edu/~mark/twc-147-2.pdf

Comments on some maybe interesteding Raku language stuff:

for (2 .. Inf).grep(*.is-prime).grep({! /0/}) -> $test {

    SNIPPETT            WHAT IT DOES
    for (2 .. Inf)      let $_ go from 2 to infinitiy...it doesn't compute
                        an infinite amount of stuff this is "lazy" and just
                        computes stuff as it needs it...when $test is used
                        the first time a new $test is computed...having
                        laziness is a feature I use a lot
    .grep(*.is-prime)   only use prime numbers
    .grep({! /0/})      the prime numbers must not contain zero
    -> $test            put each of the primes without 0 in $test
                        one at a time

    It is common for me to use this general setup of .grep(...),
    .grep(...), .grep(...) to only pick out the wanted elements so I
    don't need to put code inside the loop to pick wanted elements or
    eliminate non-wanted elements.
    
my $p := 0, 1, 5, -> $a, $b { 2*$b - $a + 3 } ... Inf;

    SNIPPET     WHAT IT DOES
    my $p :=    compute a lazy array $p[0] = 0
     0, 1, 5, -> $a, $b { 2*$b - $a + 3 }
                $p[0] = 0, $p[1] = 1, $p[2] = 5,
                $p[3] for example, is 2*$p[2] - $p[1] + 3
     ... Inf;   and do it lazily to infinity
     
(1 .. $n).map({%is-pentagonal{$p[$_]} = True});

    SNIPPETT            WHAT IT DOES
     for (1 .. $n)      let $_ go from 1 to $n
     map({%is-pentagonal{$p[$_]} = True})   set the %is-petagonal hash
                                            element $p[$_} to True

-mark


More information about the Purdue-pm mailing list