summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--raku/luhn/Luhn.rakumod9
1 files changed, 8 insertions, 1 deletions
diff --git a/raku/luhn/Luhn.rakumod b/raku/luhn/Luhn.rakumod
index 4b704b5..8efafc0 100644
--- a/raku/luhn/Luhn.rakumod
+++ b/raku/luhn/Luhn.rakumod
@@ -1,4 +1,11 @@
 unit module Luhn;
 
-sub is-luhn-valid(Str $input is copy --> Bool) is export {
+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;
 }