Learning Elixir

For the past few weeks I've been off and on learning the new hot-on-the-block functional programming language, Elixir. As such, I've been going through their getting started guide in order to familiarize myself with the basic workings of the language. However, once I was finished with the basics I started branching out and exploring the Elixir ecosystem in order to see what kind of libraries exist.

Mix it Up

Mix is Elixir's dependency and project management tool. For Rubyists, it takes the place of Ruby Gems and Bundler. So if you're going to pulling packages from Hex, Elixir's package repository, then Mix is going to be your huckleberry. If you're curious, there is a great getting started guide included in Elixir's default documentation. While the documentation is great, there is one aspect of the tool that the it wasn't clear on: How do you use it to write a one off script that depends on a package from Hex?

The problem is that there is no global space for Elixir libraries; in order to use a dependency, it has to exist within the context of a Mix project. That's totally fine though! The solution is to create a minimal Mix file that only defines the project namespace and required dependencies. Then have that file exist within the directory that you're writing your script in.

An Example

While I'm learning this 'pretty neat' programming language, I'm submitting code to my code samples repository. It's here that I've included an example on how to write a minimal script using Mix. Just check out the elixir directory.

Steps

Create a directory for your script.

Create a Mix project file. Example

mix deps.get to pull down your dependencies.

to pull down your dependencies. mix deps.compile to compile your dependencies.

to compile your dependencies. mix run my_script.exs to run your code within the context of your dependencies.

And you're done! I found this to be a lot cleaner and simpler than creating a project via mix new because I didn't want to deal with all the cruft that it created. Cruft that is needed for a 'real' project but not for a few lines of code to demo a library.

Instead of this:

* creating README.md * creating .gitignore * creating mix.exs * creating config * creating config/config.exs * creating lib * creating lib/kv.ex * creating test * creating test/test_helper.exs * creating test/kv_test.exs

I get a single file that specifies the dependencies for my one off piece of code. So I get to play with a library without having to commit to creating a 'project.'

Fun, huh?