I've opened up the elixir repl, iex, and have my stack of "easy" to answer cards from the previous post, which have instructions on the answer side of the card. GAME 2 is writing code based on the instructions and then checking it against the question code on the card. The goal being to use the flash cards to apply spaced repetition to learn how to code elixir too.

REPL = Read, Explain, Put down some code, Learn!

Here are my notes as I was going through this process.

Calling a function that accepts an argument without an argument results in a "no method error". When in a ruby REPL I rely on the error messages to guide me: "does this method even exist?", which caught me out in iex. This indicates that the arity makes the method unique.

In GAME 2 where you work in reverse, using the coding instruction to write the "question" code in an interactive shell, I found that after getting each question correct, and before moving onto the next card, I would experiment with the method a little, to find it's boundaries and quench my curiosity. This was a fantastic way for delight to emerge from practice.

After writing the code for a GAME 2 question, I found that I was not checking my answers. I would read the instructions, figure out the code and then pick up the next card. After writing the code, and seeing it work, I felt no need to double check that I had written what was asked of me.

Log a message in each level of logging. Where is this output?

This time I did rely on Elixir error messages to guide me to the correct answer. I first tried writing Log.warn "AHHH!" and got undefined method, then tried Logger.warn "boo" which told me:

require Logger before invoking the macro Logger.warn/1

This was a fantastic error message! I wrote require Logger and repeated my previous line of code and it worked. I also discovered that Logger.warn is a macro, which I know is a meta programming term for generating code, but I don't know how this works in elixir - yet!

Use assignment deconstruction to assign the tail of an array to a local variable

The first GAME 2 card I got wrong. I did know that the left hand side array should use the pipe syntax to separate the arguments. Experimenting with this, I managed to get an error message if I tried using multiple pipes

iex(2)> [h | m | t] = [1,2,3]

** (CompileError) iex:2: undefined function |/2

    (stdlib) lists.erl:1354: :lists.mapfoldl/3

    (stdlib) lists.erl:1355: :lists.mapfoldl/3

So from this I can guess that this pipe ("|") is a function that performs a map fold? What is folding a map? I cannot remember. Lets log this as a question to come back to: Log.debug "what does map fold do?".

I wrote a function that takes two arguments and found that unlike ruby's lambda syntax ->(1,2) { 1+2 } the arguments in elixir come before the arrow fn 1, 2 -> 1 + 2 end.

Use a function that checks if a string ends with 'ing'

I was pleasantly surprised that I had gotten the right function String.ends_with? but I had called it with two arguments String.ends_with?("exciting", "ing"), whereas the question code used the |> pipe operator to construct the code with partial application: "exciting" |> String.ends_with?("ing").

Pattern matching...! I could not recall how on earth to do this "easy" card in code. I had to copy, character by character, the code on the question side and run it to see it in action. This card I will be revisiting soon, to see if I can remember the syntax.

Hello Enum! Another piece of code I could not write without looking at the question code

Sort an array of integers

Array.sort/1 is a private method the compiler tells me.

Wow, I am not using the |> pipe operator nearly enough! The question code is showing me good habits to develop. After writing

String.replace("Hello. I am cool", ".", "!")

I thought it didn't look very succinct, however, the question code uses the pipe operator to make this much cleaner

"Hello. I am cool" |> String.replace(".", "!")

So, in my spaced repetition stacks, I have three levels: wrong; right but not best practice; and correct.

Ternary conditions use do: .. else, that was a shocker!

Phew! I made it through all of the 26 "easy" cards in GAME 2 mode, and have learnt how to write some ELIXIR! I feel pretty stocked about that. The cards I got wrong I am determined to turn into long term memory code that I can call on at will.

Day 4 highlights the necessity of practice frequency.

< Previous