Skip to content

Instantly share code, notes, and snippets.

@ryangraham
Created May 20, 2020 06:19
Show Gist options
  • Save ryangraham/b2366e662d7860cdf0dcb968a6e71aa5 to your computer and use it in GitHub Desktop.
Save ryangraham/b2366e662d7860cdf0dcb968a6e71aa5 to your computer and use it in GitHub Desktop.
rtn checksum in elixir
"325081403"
|> String.graphemes() # => ["3", "2", "5", "0", "8", "1", "4", "0", "3"]
|> Stream.map(&String.to_integer/1) # => [3, 2, 5, 0, 8, 1, 4, 0, 3]
|> Stream.zip([3, 7, 1, 3, 7, 1, 3, 7, 1]) # => [{3, 3}, {2, 7}, {5, 1}, {0, 3}, {8, 7}, {1, 1}, {4, 3}, {0, 7}, {3, 1}]
|> Stream.map(fn {a, b} -> a * b end) # => [9, 14, 5, 0, 56, 1, 12, 0, 3]
|> Enum.sum() # => 100
|> rem(10) == 0 # => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment