NullOranje
Yaq - An Enumerable Queue for Elixir
While working on a work project, I found I needed a more Elixir-like queue versus the Erlang :queue module. So I decided to write one. I liked what I did, so I’m sharing it with others.
Yaq (Yet another queue) is a double-ended queue that supports both the Enumerable and Collectable protocols. I wanted something I could use with the pipe operator, since with my use case I found myself having to write a lot of functions to make :queue work correctly.
This is my first library I’m sharing with the community. I not only wanted a library I could use in my day job, but something I could take an opportunity to learn how to “do things right” so with regards to Elixir.
Source code is available on Github: GitHub - NullOranje/yaq: Yet another queue module for Elixir · GitHub
Hex package: yaq | Hex
Documentation: API Reference — yaq v1.2.0
Any and all feedback is welcome.
Most Liked
kip
NobbZ
NobbZ
I get a 404 for the GitHub repository.
Also from the examples in the documentation, I really do not like that its inspection shows the length of the queue but not the contents…
Oh, and I just realise, Yaq.value/0 is nil | term, term includes nil, so why is nil mentioned extra here?
Why is there no Yaq.t/1 which would allow us to specify the members types as well?
Last Post!
NullOranje
I’ve had this same though about using atoms as markers. It’s a pretty common pattern in Erlang/OTP from what I can see, but the scenario you describe seems likely, especially when using common atoms like :ok (or nil).
I liked your suggestion about following the get/fetch/fetch! paradigm in Map and elsewhere, so I added a couple functions to the API:
fetch/1andfetch_r/1will return the tuple{value, updated_queue}if there are elements on the front or back of the queue, respectively, or:errorotherwisefetch!/1andfetch_r!/1will return the tuple{value, updated_queue}if there are elements on the front or back of the queue, respectively, or raiseYaq.EmptyQueueErrorotherwise
I also added a default value specification for dequeue/2 and dequeue_r/2 to follow the pattern from get.
You can only enumerate and collect from front to back, but you can reverse the queue in constant time with Yak.reverse/1, so I think it is a fair compromise.
Popular in Announcing
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









