Created
May 21, 2020 03:56
-
-
Save ryangraham/68a0e470dbf9f93859f67c8cc451b838 to your computer and use it in GitHub Desktop.
luhn range-v3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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