summary refs log tree commit diff stats
path: root/raku/luhn/Luhn.rakumod
blob: 2bdaf16ae790a3007435b51164741d3b6b350f0f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
unit module Luhn;

sub is-luhn-valid(Str $input --> Bool) is export {
    with $input.comb(/\d|\s/) -> @num-spc {
        return False if @num-spc.elems !== $input.chars;

        my Int @numbers = @num-spc.comb(/\d/)>>.Int;
        return False if @numbers.elems < 2;

        ([+] gather for @numbers.reverse.kv -> $k, $v {
             take $k !%% 2 ?? subtract-if-greater($v * 2, 9) !! $v;
         }) %% 10
    }
}

#| subtract-if-greater will subtract $to-sub from $num if $num is
#| greater than $to-sub.
sub subtract-if-greater(Int $num, Int $to-sub --> Int) {
    $num > $to-sub ?? $num - $to-sub !! $num
}