Some Quick Answers

In Day Two - Import, Alias and More I talked a little bit about DailyDrip.com and some questions I had. Thanks to speedy feedback from Josh, I got my answers. If you’re interested, you can read my review of the service here: DailyDrip Elixir Training Review.

Bite-Sized Learning & Prep Work

Over the next couple of days I’m doing a Node JS workshop, so my time is going to be fairly limited. I’m also getting prepared to start learning Phoenix through a new learning service (review coming).

One of the things I’m starting to get familiar with is MIX, which is a cool build tool for Elixir.

Check out more here: Mix - Elixir Build Tool

Once you setup a project in Mix, you end up with a Mix.exs file. And in that file, you can manage dependencies (reminds me of NPM a little).

For example, to add dependencies, open your Mix.exs file and add them in the deps function like this:

1
2
3
defp deps do
[{:timex, "~> 2.1.4"}]
end

The above code, which is a tuple with an atom and version information, will add the timex package to your dependencies. And according to the docs, Mix is using HEX, which is a package manager, to fetch dependencies.

I actually wasn’t sure where the docs were for versioning (“~>, =, etc”). I went through Mix and Hex docs and eventually found the answer over at the Elixir lang docs. Here are some useful examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# only the version 3.0.5
== 3.0.5
# anything larger than version 1.8.3
> 1.8.3
# version 2.1.0 and beyond
>= 2.1.0
# version 2.0.0 to 2.0.x
~> 2.0.0
# version 2.1.2 to 2.1.x
~> 2.1.2
# version 2.0 to 2.x
~> 2.0
# version 2.1.3-dev to 2.1.x
~> 2.1.3-dev

For more information, check out the Elixir version doc.

Ok, so you’ve added some dependencies, now what?

By adding them to the application function, you can make sure they get loaded when your app starts.

1
2
3
def application do
[applications: [:logger, :timex]]
end

As you can see, you just list the atom for the dependency in a list for applications.

A Little Erlang

I spent time in IEX playing around with Erlang’s Enum.Filter, which filters an enumerable and returns a truthy value.

Here’s a little example that takes a list of numbers, filters them and hands back a list of numbers that are each over 57.

1
2
# will return [87, 99, 194]
Enum.filter([87, 45, 22, 12, 99, 194], fn(x) -> x > 57 end)

On a side note, I added a string into that list (“lolz”) and it came back after the filter. I was thinking that since it wasn’t an Integer it would just be filtered out.

I then when into IEX and tried this:

1
2
3
4
5
6
7
8
9
10
11
iex> x = "lolz"
# x is now "lolz"
iex> x < 1
# false
iex> x > 27346287346782648762
# true

So I’m kind of wondering why “lolz” > 27346287346782648762 is true. I’m sure it’s something simple.

update to above string issue: So one thing I was missing is that in Elixir a “string” is a binary. For example:

1
2
3
4
# binary
x = <<104, 101, 108, 108, 111>>
# x is now "hello" :)

My To-Do List

From here, I have several items to do over the coming few days for learning and for this site.

  • Continue evaluating Daily Drip
  • Begin evaluating LearnPhoenix.io
  • Get back into (and start review for) Programming in Elixir 1.3
  • Explore Mix and Hex a little more – eventually, I want to do a series just on them as I like building tools and package managers.
  • Finish up the Red:4 video and finish the review
  • Go through this site and make edits

Until next time, keep your functions pure!