ISBN13 check digit: Difference between revisions
Content added Content deleted
Langurmonkey (talk | contribs) |
Langurmonkey (talk | contribs) |
||
Line 188: | Line 188: | ||
=={{header|langur}}== |
=={{header|langur}}== |
||
{{works with|langur|0.7.1}} |
{{works with|langur|0.7.1}} |
||
In this example, we map to multiple functions (actually 1 no-op). |
|||
<lang langur>val .isbn13checkdigit = f(var .s) { |
<lang langur>val .isbn13checkdigit = f(var .s) { |
||
matching(re/^[0-9]{13}$/, .s = replace(.s, RE/[\- ]/)) and |
matching(re/^[0-9]{13}$/, .s = replace(.s, RE/[\- ]/)) and |
||
fold(f{+}, map([_, f .d x 3], map(f .c-'0', s2cp .s))) rem 10 == 0 |
fold(f{+}, map([_, f .d x 3], map(f .c-'0', s2cp .s))) rem 10 == 0 |
||
} |
|||
val .tests = h{ |
|||
"978-1734314502": true, |
|||
"978-1734314509": false, |
|||
"978-1788399081": true, |
|||
"978-1788399083": false, |
|||
} |
|||
for .key in sort(keys .tests) { |
|||
val .pass = .isbn13checkdigit(.key) |
|||
write .key, ": ", if(.pass: "good"; "bad") |
|||
writeln if(.pass == .tests[.key]: ""; " (ISBN-13 CHECK DIGIT TEST FAILED)") |
|||
}</lang> |
|||
{{works with|langur|0.7.0}} |
|||
In this example, we set a for loop value as it progresses. |
|||
<lang langur>val .isbn13checkdigit = f(var .s) { |
|||
var .odd = true |
|||
matching(re/^[0-9]{13}$/, .s = replace(.s, RE/[\- ]/)) and |
|||
for[=0] .d in map(f .c-'0', s2cp .s) { _for += if(.odd: .d; .d x 3); .odd = not .odd } rem 10 == 0 |
|||
} |
} |
||