Skip to content

Instantly share code, notes, and snippets.

@ryangraham
Created May 21, 2020 03:56
Show Gist options
  • Save ryangraham/68a0e470dbf9f93859f67c8cc451b838 to your computer and use it in GitHub Desktop.
Save ryangraham/68a0e470dbf9f93859f67c8cc451b838 to your computer and use it in GitHub Desktop.
luhn range-v3
bool luhn_check(const std::string& pan) {
auto to_int = [](char c) { return c - 48; };
auto sum_digits = [](int n) { return n / 10 + n % 10; };
auto do_digits = [&sum_digits](auto&& values) {
auto const& [index, digit] = values;
return (index % 2 == 0) ? digit : sum_digits(digit * 2);
};
auto rng = pan
| views::transform(to_int)
| views::reverse
| views::enumerate
| views::transform(do_digits);
auto sum = accumulate(rng, 0);
return sum % 10 == 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment