summary refs log tree commit diff stats
path: root/raku/luhn/Luhn.rakumod
blob: 8efafc050b7c8f7e7499ce990a8ad2e3acd661a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
unit module Luhn;

sub is-luhn-valid($input is copy --> Bool) is export {
    return False if $input.comb.grep(/\d|\s/).join ne $input;
    $input = $input.comb.grep(/\d/)>>.Int;
    return False if $input.elems < 2;

    ([+] gather for $input.reverse.kv -> $k, $v {
         take $k !%% 2 ?? ($_ > 9 ?? $_ - 9 !! $_) !! $v with $v * 2;
     }) %% 10;
}