Luhn
Decide whether a string of digits and spaces passes the Luhn checksum.
Adapted from Exercism’s problem specification (MIT). Listed under Parsing & Validation.
// FILE: luhn.nomi
//# Validates numbers with the Luhn checksum.
/// Reports whether `number` passes the Luhn checksum. Spaces are ignored;
/// any other non-digit, or fewer than two digits, makes it invalid.
pub fn is_valid(number: String) -> Bool
digits =
number
|> String.replace(" ", "")
|> Iter.map(String.to_int)
|> Maybe.collect()
case digits
Some(digits) when Iter.count(digits) > 1 -> checksum(digits) % 10 == 0
_ -> False
end
end
fn checksum(digits: List<Int>) -> Int
digits
|> Iter.reverse()
|> Iter.with_index()
|> Iter.map(fn (i, d) -> if i % 2 == 1 then double(d) else d)
|> Iter.sum()
end
fn double(digit: Int) -> Int
doubled = digit * 2
if doubled > 9 then doubled - 9 else doubled
end
// FILE: luhn_test.nomi
import luhn
test "validates numbers with the Luhn checksum"
[
("single digit strings can not be valid", "1", False),
("a single zero is invalid", "0", False),
("a simple valid SIN that remains valid if reversed", "059", True),
("a simple valid SIN that becomes invalid if reversed", "59", True),
("a valid Canadian SIN", "055 444 285", True),
("invalid Canadian SIN", "055 444 286", False),
("invalid credit card", "8273 1232 7352 0569", False),
("invalid long number with an even remainder", "1 2345 6789 1234 5678 9012", False),
(
"invalid long number with a remainder divisible by 5",
"1 2345 6789 1234 5678 9013",
False,
),
("valid number with an even number of digits", "095 245 88", True),
("valid number with an odd number of spaces", "234 567 891 234", True),
("valid strings with a non-digit added at the end become invalid", "059a", False),
("valid strings with punctuation included become invalid", "055-444-285", False),
("valid strings with symbols included become invalid", "055# 444$ 285", False),
("single zero with space is invalid", " 0", False),
("more than a single zero is valid", "0000 0", True),
("input digit 9 is correctly converted to output digit 9", "091", True),
("very long input is valid", "9999999999 9999999999 9999999999 9999999999", True),
("valid luhn with an odd number of digits and non zero first digit", "109", True),
("using ascii value for non-doubled non-digit isn't allowed", "055b 444 285", False),
("using ascii value for doubled non-digit isn't allowed", ":9", False),
(
"non-numeric, non-space char in the middle with a sum that's divisible by 10 isn't allowed",
"59%59",
False,
),
]
|> subtests (description, number, expected), description
assert luhn.is_valid(number) == expected
end
endok validates numbers with the Luhn checksum (22 cases)
test result: ok. 22 passed, 0 failed
How it’s written
Section titled “How it’s written”- Each grapheme goes through
String.to_int, andMaybe.collectturns “every one is a digit” into oneMaybe<List<Int>>, so a stray character and the digit conversion are one step. - The length rule is a
whenguard on theSomearm rather than a separateif. checksumreverses the digits and doubles odd indexes fromIter.with_index, rather than tracking a parity flag in a fold, then totals them withIter.sum.- All 22 cases are one
subteststable whose first column is the canonical description, which names each row.