The first code runs nicely as a Livebook (v0.10.0) page. It recognizes American Express, Discover, Mastercard, and Visa payment card account numbers (PANs).
After being away from Elixir for a few weeks I thought it would take awhile to get back into the functional mindset, but I found it took less than an hour to start writing reasonable (I hope) code.
Pattern matching drives everything. It accounts for well over 90% of the code. A significant departure from the implementations I’ve done in Python, Go, Javascript, Delphi, Visual Basic, etc. I thought less about algorithms and more about the shape of the data.
The first step converts the string (PAN) into a list. I started by using String matching to distinguish card brands but found that approach led to more String handling in the private functions. Doing the conversion up front led to some wordy pattern matches, but less code.
I’m already writing
defguard
macros. I’ve considered macros a fairly advanced topic and have just skimmed those chapters.The macro simplified an otherwise wordy guard. Still, I know to use cautiously.
Next Steps
Change the
pan_range
parameter order to putpan_length
first.Add guards to
card_brand
to check for String and Integer.Add China UnionPay, Diners, JCB, Maestro, and Visa Electron.
Proper Mix project (including tests).
More brands.
Publish(?)
Yes, I changed SimpleCardBin to SimpleCardBrand, much better name.