Learn All The Things

Today I learned mostly about putting things together in Elixir along with a little taste of debugging.

On the way home, I heard about Daily Drip, which looks to be a teach service that sends you bites of knowledge to your inbox throughout the week. I believe it’s from the same guy who does Elixir Sips, but I’m not 100% sure.

Daily Drip is $9 per month (or $90 per year) for Elixir as of this writing. There’s also Elm and a couple other things (price is $240 per year for everything).

The main questions I’m having right now are:

  • Is the content any good?
  • Is the content from Daily Drip the same content that’s on Elixir Sips?
  • How current is said content?
  • Is the content from a sole creator or multiple sources?

I’m going to reach out to the owner to get the answers to these questions, so look for more on this issue soon. For $9 a month, I’ll gladly take a stab at it and report my findings here.

Putting It Together

Ok, so let’s get into actual Elixir. Tonight I mainly worked on getting through a little more of the Red4, which focused on how code is pieced together.

File Extensions

For starters, there are two different file extensions for Elixir files: .exs and .ex.

.exs = file’s contents will be executed and checked at runtime. A good example of this type of file is your test files.

.ex = file will be compiled.

Bring in the Code

So unless you’re going to be the fun guy at the office party who puts all his code into a single file (OMG, please don’t), you’re going to need a way to import/export code here and there. Let’s jump into some ways to make that happen.

Here are four ways to import code into a module:

ALIAS

You can use alias to import a module and then rename it to use it with the desired namespace.

1
alias Master.Item, as: FizzBuzz

Now you can access Master.Item as FizzBuzz.

Also you can use Alias to shorten the call to the module.

1
2
# After this alias, you can use just Item now.
alias Master.Item

REQUIRE

I expected require to import some code and break if the code wasn’t there, but I was wrong (kind of).

Require statements are actually used for metaprogramming where you can define macros that end up being injected at compile time.

At this time, I don’t know anything about macros/meta in Elixir, so I’ll just leave this for now.

1
require IEx

IMPORT

When you use an import statement, you’re taking the functions from the imported module and putting them directly into the module that’s doing the calling. Be careful though as it’s possible to end up with namespace conflicts if you’re not careful. You can also extract certain functions and exclude others.

1
2
3
4
5
6
7
8
9
10
# blanket import
import Converter
# import specific functions by calling them along with their Airity.
# Side note: Airity is the number of arguments a function takes.
import Converter, only: [my_function: 2]
# import everything except certain functions
# again, you need to list the functions and their airity
import Converter, except: [I_hate_this_function: 8]

USE

Use is interesting to me. It’s a macro that does a REQUIRE on a named module AND fires a method on that module called using. What’s extra interesting is that you can pass in a keyword list with the statement.

1
2
3
4
5
# normal
use ExUnit.Case
# pass in options
use ExUnit.Case, async: true

For more information on alias, import, require, and use: check out Elixir Lang: alias, require and import

Debugging

Tonight I was also introduced to PRY, which is pretty sweet. It allows you to pry into a function to see the values of the argument(s) passed into it. Fire up your IEX shell and try this out.

1
2
3
4
5
6
7
defmodule Demo do
require IEx
def fun(x, y) do
IEx.pry
x + y
end
end

Now, type this into your shell and select Y for yes when asked.

1
iex> Demo.fun(6, 3)

After typing Y, you’ll see a pry prompt. Type in x or y and you’ll be given the values of those variables. Awesome!