FEATURED POSTS
PAGES
Getting Going
note: I tend to write the journal entries a day or so after the event.
On Day Zero I basically did some setup, made this blog and did some light reading. Today was the first "real" day of learning Elixir.
These journals are generally just me putting down what I've learned or places I've explored.
Video
While strolling through Elixir Forum I found a couple posts talking a Red4 video, so I decided to check it out. The sample was pretty good and entertaining, so I paid $25 for it. You can read my review for it here: Red4 Training Video Review.
Today I worked through about 40% of the video and began building out the example app. It was great seeing some scaffolding and seeing how data flows through the functions.
If you're someone who has income to spend on training, I recommend checking it out.
Podcast
I learned about the Elixir Foundation Podcast last night and subscribed on my Android phone. Today on the way home from work, I listened to the latest episode (Elixir Fountain Ben Marx 2016-08-16). It was pretty good. I recommend checking it out. I'll be working my way through the past episodes this week.
Site
I signed up over at Exercism.io and got everything all setup. I did the first exercise, which was a "Hello, World!" with a little twist. While I did it properly, I soon learned about how string interpolation is done in Elixir:
"Hello #{some_var}!"
My original solution wasn't as elegant:
# hello(), hello("Jason"), etc
def hello(name \\ "World") do:
"Hello " <> name <> "!"
end
As time permits, I plan to work my way through the other problems.
Learning
I learned a good bit of Elixir so far and shared what I learned with some co-workers. Here are some of the key points:
Equals !== Assignment
When I first saw x = 5 I thought it was assignment. Nope, it's equality. And this really didn't sink in until I saw the following code in the Red4 video:
iex> pat <> x = "pattern match"
# x = "tern match"
It's Algebra again.
By the way, <> is a string concatenation operator.
Pattern Override
Since Elixir does pattern matching, you can have function override by passing in different types. For example:
def my_function(:miles) do
...
end
def my_function(:meters) do
...
end
If you call that function with a :miles ATOM, it will use the first one. And if you use a :meters instead, it will use the second one.
My Type of data
Elixir types:
{tuple}
%{map}
%Struct{key: val}
:atom
[L,I,S,T]
Atom: The name of the atom is its value. Example:
:blue
:red
:ok
:fail
Tuple: It's a collection of things. Generally a tuple is going to be up to 4-5 items, because after that, you'll probably look into using a MAP. Also (as far as I know), it's common for the first item of a tuple to be an atom, which describes the data of the tuple. Example:
tuple = {:stuff, 55, "Phoenix", [1, 88, 34] }
List: It's a list of things and while it may look like an array, it isn't really one. Example:
list = ["Jason", 3847, ["Jeep", "Elixir", 11]]
Keyword List: This is a list with labels (and those labels are atoms). Example:
my_list = [vehicle: "Jeep", location: "Vegas", days: 13]
Maps: It’s like a hash. Maps have any value as a key. Keyword lists must have an ATOM as a key. Example:
map = %{key: “value”, another_key: “another value”}
# call like this:
map.key == “value"
Structs: Structs have Predefined keys and the benefit of default values along with type safety. Example:
struct = %Table{color: :brown, legs: 4}
Matter of Style
I learned another way to call functions, which I like.
Original:
my_function({:miles, 2})
Improved:
{:miles, 2} |> my_function
I find that it's easier to read and cleaner. Speaking of cleaner, I also like this method of piping functions:
# Just using a list so you get the idea
{:gadgets, [list, of, gadgets, here]}
|> remove_broken_items
|> remove_smart_phones
|> calculate_total_expenditure
And lastly in the style department, I learned a second way to write out functions.
Normal:
def my_function(name) do:
...
end
Optional Method:
def my_function(name), do: "Hi #{name}!"
Closing Thoughts
Over the past day and a half I signed up for many things and have been spending time ready Slack messages, forum posts and listening to a couple podcasts. So far, I'm enjoying life and looking forward to improving.
Next up I'll be looking into some other training offerings and continuing on the Red4 video. Stay tuned!