← All series

Series contents

Log in to track your progress

  1. 1. Welcome to Alchemist Camp Episode
  2. 2. Lesson 1: The guessing game Episode
  3. 3. Solution 1: The Greeter Episode
  4. 4. Lesson 2: Word Count Episode
  5. 5. Word Count ULTIMATE EDITION! Episode
  6. 6. Minimal Todo List Part 1 Episode
  7. 7. Elixir lists vs Elixir tuples Article
  8. 8. The Image Snatcher Episode
  9. 9. Minimal Todo List Part 2 Episode
  10. 10. Elixir maps made effortless Article
  11. 11. Fibonacci with tail recursion Episode
  12. 12. Mini Markdown Episode
  13. 13. Extending Mini Markdown Episode
  14. 14. Alchemy Markdown Episode
  15. 15. Using mix to modularize and add tests Episode
  16. 16. Command line applications with escript Episode
  17. 17. Caboose, a simple Unix tail clone Episode
  18. 18. Game board (Tictac Part 1) Episode
  19. 19. Structs: Maps with checks and default values Article
  20. 20. Deck generation, Pythagorean Triplets and more Episode
  21. 21. Whiteboarding the Tictac state machine Episode
  22. 22. Game state machine (Tictac Part 2) Episode
  23. 23. Modelling the memory game Episode
  24. 24. Finishing Tictac and making a CLI Episode
  25. 25. Threatened by rooks, bishops and knights Episode
  26. 26. A worker to log stats from a YouTube’s API Episode
  27. 27. Constructing bitmap files with Elixir Episode
  28. 28. Making bitmaps with color palettes Episode
  29. 29. Flattening lists recursively in Elixir Episode

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)

Open standalone page ↗

8 Comments

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.

- jtoulouse · 6 years ago

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.

- alchemist · 6 years ago

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.

- czio · 6 years ago

Could you share the snippets file that you show in the beginning?

- yoleg · 6 years ago

How can I see full code?

- drop of elixir · 2 years ago

Great episode!

- kokoboko · 5 years ago

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'

- ShanSiddiqui94 · 4 years ago