Our third project is considerably more ambitious than the first two.
We'll build a command line Todo list app that reads CSVs, parses them into an Elixir map, and takes commands from user input. To be clear, this is a ridiculous project. There are numerous todo lists written in various JavaScript and mobile development frameworks but who has seen a command line todo list?
The plan (5:00)
- ask user for file name
- open file and read it
- parse the data
-
ask user for command
- read todo
- add todo
- delete todo
- load file
- save file
- quit
New language features covered
Tuples: Like lists, tuples are ordered sets of elements. Unlike lists, tuples are set contiguously in memory. This makes looking up elements in tuples cheap, whereas with a list, looking up the 8000th item involves traversing the 7999 items before it. On the other hand, copying is expensive for tuples but cheap for lists.
Atoms: Atoms in Elixir are unique symbols. They are used internally for naming modules and are very commonly used in response codes such as :ok
or :error
. We'll be pattern matching against these atoms frequently in the future.
(Source code available for premium members)
Building our todo list (10:24)
The basic idea of our program is that it will always be running in a loop after the initial start function.
There is a get_command
function that prints the available commands to the user and listens to their keystroke. Invalid commands just re-display the command options. When the user enters a command, it is handled and then get_command
is called again to get their next command. The loop continues forever until they enter the command to quit.
State is stored as an Elixir Map
and passed into functions as an argument. Updating the todo list is as simple as reading in the data passed to a function, updating (or actually creating a new Map since Elixir variables are immutable) and then passing it into the next function call like so: get_command(new_data)
.
By the end of the episode, we have start
, quit
, show_todos
and delete_todo
implemented. We'll get to the rest of the functionality next episode.
Challenge 3
Make an Elixir program that looks at all the files in the current directory and then moves all files ending in .jpg, .gif, .bmp or .png to a new sub-directory called "images".
Hint: This will be much easier if you look at the docs for Enum and see what Enum.each and Enum.at do.
(solution here)
8 Comments
Could you share the snippets file that you show in the beginning?
With VSCode, I now use this extension:
ElixirLS Fork: Elixir support and debugger elixir-lsp.elixir-ls
. Far better than the others I tried until now.The ElixirLS Fork is now the same as the original ElixirLS plugin. There was a divergence due to the original creator taking a break but everyone got together and unified the project again.
At some point close to 30th min, it felt hard to follow because terminal seems that was overused instead of writing code in the editor.
@yoleg Here are some of my snippets: https://gist.github.com/AlchemistCamp/7b9ca48a39846556adfa290be7d9f445
Great episode!
I followed your tutorial to the T. But when I go to "def Parse" none of the commands worked for me after that, no matter what I did my terminal returned Compile Error Undefined functions 'there is no such import'
How can I see full code?