-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OPQ.Queue
wraps :queue
and implements Enumerable
- Loading branch information
Showing
8 changed files
with
89 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
defmodule OPQ.Queue do | ||
@moduledoc """ | ||
A `:queue` wrapper so that protocols like `Enumerable` can be implemented. | ||
""" | ||
|
||
@opaque t() :: %__MODULE__{data: :queue.queue()} | ||
|
||
defstruct data: :queue.new() | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defimpl Enumerable, for: OPQ.Queue do | ||
@moduledoc """ | ||
Implementation based on https://github.com/princemaple/elixir-queue | ||
""" | ||
|
||
def count(%OPQ.Queue{data: q}), do: {:ok, :queue.len(q)} | ||
|
||
def member?(%OPQ.Queue{data: q}, item) do | ||
{:ok, :queue.member(item, q)} | ||
end | ||
|
||
def reduce(%OPQ.Queue{data: q}, acc, fun) do | ||
Enumerable.List.reduce(:queue.to_list(q), acc, fun) | ||
end | ||
|
||
def slice(%OPQ.Queue{}), do: {:error, __MODULE__} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defimpl Inspect, for: OPQ.Queue do | ||
@moduledoc """ | ||
Implementation based on https://github.com/princemaple/elixir-queue | ||
""" | ||
|
||
import Inspect.Algebra | ||
|
||
def inspect(%OPQ.Queue{} = q, opts) do | ||
concat(["#OPQ.Queue<", to_doc(Enum.to_list(q), opts), ">"]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters