SimpleCardBrand

Macros already?

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.

[Read More]

SimpleCardBrand

I’ve selected a small Elixir project to write between wrapping up one Python project and starting on the next. The SimpleCardBrand project will return the bank-card type based on the first six digits in the card.

A quick hex.pm search found packages that return more detailed information, but I didn’t find anything that ran without calling a web service.

I like this for a quick project as it can easily be done in a LiveBook and it primarily relies on function parameter pattern matching.

Let’s see where I am in a few days.

[Read More]

A Mad Dash Through Elixir Types

Elixir for Programmers, Second Edition

Notes

  1. ?≠ returns a codepoint: 8800.

  2. Dividing two integers returns a float. Use div and trunc to get integer results.

  3. In Elixir, nil is false.

  4. Regular expressions are based on PCRE (Perl lives on!).

  5. =~ performs a regular expression match

    iex> str = "once upon a time"
    iex> str =~ ~r/u..n/
    true

  6. =~ also accepts a string as its right hand argument, in which case it returns true if the left string contains the right.

  7. Lists are not arrays. You cannot calculate an offset to a specific element.

  8. map[:missingKey] returns nil. map.missingKey raises an exception.

[Read More]

Always be Normalizing

Elixir for Programmers, Second Edition

I ran into the UTF-8 normalization issue when playing around with the String.split exercises.

Some non-ASCII values can be represented by either one or two graphemes. For example ô can be a single grapheme or composed of two graphemes, o and ^. Always working with normalized UTF-8 strings avoids confusion.

There are four types of UTF normalization which you can read about on the Unicode.org site. Elixir seems to prefer the Canonical Decomposition (NFD) form as that is what the String.equivalent? function uses.

The following examples demonstrate:

[Read More]

String Splitting and Trimming

Elixir for Programmers, Second Edition

I had some confusion with the String.split function in yesterday’s exercises. The documentation says that the trim: true option removes empty strings from the resulting list.

The important term here is “empty”, not “blank” or “whitespace”. My Python-brain was still thinking of trim (or strip) options meaning whitespace is stripped from start/end of the source string.

For example:

[Read More]

Notes on Our First Project

Elixir for Programmers, Second Edition

Setting up the dictionary project as an actual separate Elixir project is a more structured approach than I’ve seen so far. Use of multiple projects has been previously discussed but examples to date have been more at the module level within a project. Excited to see this approach introduced so early in the course. Already changing my assumptions about future project construction.

Also new to me was the use of @ to run a function at compile time. I have used @ notation, but only to declare a fixed value. Not sure when I would use a dynamic compile-time value vs. a configuration, but good to know it exists.

[Read More]

Elixir for Programmers, Second Edition

Sticking with Dave Thomas for the moment and starting the Elixir for Programmers, Second Edition course. I’m expecting that much of this will be a refresher on what I’ve already covered, along with a first foray into Phoenix LiveView.

The LiveView will be a bit outdated given the recent major LiveView improvements, but mostly I’m looking for solid Elixir coding exercises.

I have a first project in mind, which does not require LiveView. Plan to start the project after I finish this course.

[Read More]