I published my first Elixir package to hex!
Tags: elixir, functional, devjournal, beginners
Published: 2022-01-19 13:30:46

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](https://www.npmjs.com/package/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](https://www.hex.pm/packages/word_list) and the source code [here](https://github.com/brew-install-buzzwords/word_list).

## The code

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

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

Additionally, I added a call to `Steam.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](https://hex.pm/docs/publish) 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?

## More Content

If you liked this, you might also like some of my other posts. If you want to be notified of my new posts, [follow me on Dev](https://dev.to/brewinstallbuzzwords) or subscribe to my [brief monthly newsletter](https://mailchi.mp/89dc077154c7/brewinstallbuzzwords).

* [What's the best questions you've been asked in a job interview?](https://dev.to/brewinstallbuzzwords/whats-the-best-question-youve-been-asked-in-a-job-interview-4a1j)
* [What was the first program you wrote?](https://dev.to/brewinstallbuzzwords/what-was-the-first-program-you-wrote-1gjp)
* [The weird quirk of JavaScript arrays (that you should never use)](https://dev.to/brewinstallbuzzwords/the-weird-quirk-of-javascript-arrays-that-you-should-never-use-4l3b)
* [Does Elixir have for loops?](https://www.brewinstallbuzzwords.com/posts/does-elixir-have-for-loops/)
* [Learn Elixir with me!](https://www.brewinstallbuzzwords.com/posts/learn-elixir-with-me/)
* [Project Tours: Bread Ratio Calculator](https://www.brewinstallbuzzwords.com/posts/bread-ratio-calculator/)
* [Changing Emoji Skin Tones Programmatically](https://www.brewinstallbuzzwords.com/posts/emoji-skin-tone/)
* [I made my first svg animation!](https://dev.to/brewinstallbuzzwords/i-made-my-first-svg-animation-29j3)
* [5 tips for publishing your first npm package](https://www.brewinstallbuzzwords.com/posts/tips-for-first-npm-package/)
* [4 Hugo Beginner Mistakes](https://www.brewinstallbuzzwords.com/posts/hugo-beginner-mistakes/)