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)
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 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 or subscribe to my brief monthly newsletter.
- What's the best questions you've been asked in a job interview?
- What was the first program you wrote?
- The weird quirk of JavaScript arrays (that you should never use)
- Does Elixir have for loops?
- Learn Elixir with me!
- Project Tours: Bread Ratio Calculator
- Changing Emoji Skin Tones Programmatically
- I made my first svg animation!
- 5 tips for publishing your first npm package
- 4 Hugo Beginner Mistakes
Top comments (0)