Our third project is considerably more ambitious than the first two.
The plan <small>(5:00)</small>
ask user for file name
open file and read it
parse the data
ask user for command
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
:error. We'll be pattern matching against these atoms frequently in the future.
(Source code available for premium members)
Building our todo list <small>(10:24)</small>
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:
By the end of the episode, we have
delete_todo implemented. We'll get to the rest of the functionality next episode.
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.