I have an urge to write my first module and methods, so I've picked out two simple functions from the Elixir fundamentals deck to reimplement:
There are a couple of cards that demonstrate a module in use, so I'm going to use these as a reference to see how far I can get:
My super useful reimplementation of
tl looks like this:
defmodule Ejixir do def j_tl(coll) do [ _h | t ] = coll t end end IO.puts Ejixir.j_tl([1, 2, 3])
Running my file with
$ elixir ejixir.exs results in... zilch! Just an empty line. What is going on?
Changing the array to use strings
["a", "b", "c"] results in
bc as the output, so numbers are not automatically cast to strings like in Ruby it seems. What I really want to show in
STDOUT when I run the file is
["b", "c"], so that I can confirm that my method is returning what I expect. Time to investigate.
From the getting started guide it becomes clear that there are two types of data,
chardata, strings and characters, which is why the numbers and square brackets were not output. To get around this we can use the
IO.inspect function which uses
iodata to output everything:
> IO.inspect [1, 2, 3] [1, 2, 3]
Now I can test that my module is working as expected by running the file:
That was fun, and now I am wondering how I could use a testing library to develop a whole bunch more of these reimplemented functions. Something for Day 7!