I published my first Elixir package to hex!

by Adam Davis

Table of contents:

As someone who primarily writes code in JavaScript, I’ve grown accustomed to npm packages existing for pretty much any use case I can come up with. But recently while working on an Elixir project, I came across a situation where an npm package I’ve used previously would be helpful, but I couldn’t find a similar package on hex.

That, of course, is one of the drawbacks to using a language that isn’t as mainstream — there are going to be fewer online resources to take advantage of when you want to find a dependency or find an answer to a question.

The problem I needed to solve

The use case for my project was fairly simple. I needed a way to import a very long list of English words, which I could then filter based on different criteria in my application.

There is a pre-existing npm package called word-list that solves this, and I’ve used it before in another project I worked on. But after doing some searching, I didn’t find anything similar on hex, Elixir’s package manager.

My solution

Since word-list had already compiled a list of English words, I decided to make a clone of that project in Elixir.

You can find my package here and the source code here.

The code

My solution contains a single function that imports the text file containing all the words and makes it available as a stream.

defmodule WordList do
  def getStream!() do
    Application.app_dir(:word_list, "/priv/words.txt")
    |> File.stream!()
    |> Stream.map(fn x -> String.trim(x) end)

Additionally, I added a call to Stream.map/2 to trim the new newline character from each word. However, since this is a Stream, that operation is performed lazily. Doing so helps avoid a long initial load time to getting the list of words, and instead pushes off the trim until a word is actually retrieved.

Publishing my package

I found that publishing to hex was pretty easy. I followed this documentation and didn’t run into any issues.

Once I registered an account with hex and filled out the appropriate fields in mix.exs, all I had to do to publish was run mix hex.publish.

Have you published to hex?

If you’ve published any packages to hex before, feel free to share them in the comments. Have you ever run into any difficulties with the process? Have you learned any techniques that streamline the process?

Adam Davis

Share this post: