Minimal Todo List Part 1

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)

Back to index