Skip to content

Instantly share code, notes, and snippets.

@habere-et-dispertire
Created November 26, 2022 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save habere-et-dispertire/d640a8f3e487df4012dfb635780e4afa to your computer and use it in GitHub Desktop.
Save habere-et-dispertire/d640a8f3e487df4012dfb635780e4afa to your computer and use it in GitHub Desktop.
[Raku] ISBN Verifier
grammar ISBN {
token TOP { <tenner> | <niner-xray> }
token tenner { [ <digit> <:Pd> ? ] ** 10 }
token niner-xray { [ <digit> <:Pd> ? ] ** 9 X }
}
class ISBN::Verify {
method TOP ( $/ ) {
make sum( $_ Z× ( 10...1 ) ) %% 11 with flat
$<tenner><digit> // $<niner-xray><digit>, 10
}
}
sub is-isbn10 ( $barcode ) is export {
ISBN.parse( $barcode, actions => ISBN::Verify )
.made
// False
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment