June 2022
Intermediate to advanced
130 pages
2h 45m
English
Let’s build a program to create all of the Roman numeral values from 1 to 1000. We could do so by adding a number to a map, one at a time, but that’s not a functional design.
There’s a better way, though. When you can, it’s better to think more broadly. When you’re building a big, predictable map, don’t build it one piece at a time. Build all of the tuples at once, and then dump them all at once into a new map.
Let’s take an example. Create a new mix project called Romans, like this:
| | [elixir] ➔ mix new roman |
| | ... |
| | [elixir] ➔ cd roman |
| | [roman] ➔ |
Now, let’s think a bit. We’ll want to build a function to calculate the first Roman digit from the left, given a decimal number. The code will look like this:
| | def digit(number) ... |
Read now
Unlock full access