Packages
An Elixir wrapper around the Erlang optimized `queue` that supports the FIFO, first-in first-out, pattern. This is useful is when you can't predict when an item needs to be taken or added to the queue. Use this instead of using `++` or double reversing lists to add items to the "back" of a queue.
Current section
2 Versions
Jump to
Current section
2 Versions
Compare versions
4
files changed
+57
additions
-55
deletions
| @@ -24,8 +24,6 @@ IO.puts "#{inspect :timer.tc(EQueue.Performance, :test_queue, [EQueue.new])}" | |
| 24 24 | |
| 25 25 | ## Installation |
| 26 26 | |
| 27 | - If [available in Hex](https://hex.pm/docs/publish), the package can be installed as: |
| 28 | - |
| 29 27 | 1. Add e_queue to your list of dependencies in `mix.exs`: |
| 30 28 | |
| 31 29 | def deps do |
| @@ -11,4 +11,4 @@ | |
| 11 11 | {<<"maintainers">>,[<<"Benjamin Falk">>]}. |
| 12 12 | {<<"name">>,<<"e_queue">>}. |
| 13 13 | {<<"requirements">>,[]}. |
| 14 | - {<<"version">>,<<"1.0.0">>}. |
| 14 | + {<<"version">>,<<"1.0.1">>}. |
| @@ -19,9 +19,9 @@ defmodule EQueue do | |
| 19 19 | @doc """ |
| 20 20 | Returns an empty queue |
| 21 21 | |
| 22 | - == Example |
| 23 | - iex> EQueue.new |
| 24 | - #EQueue<[]> |
| 22 | + ## Example |
| 23 | + iex> EQueue.new |
| 24 | + #EQueue<[]> |
| 25 25 | """ |
| 26 26 | @spec new :: EQueue.t |
| 27 27 | def new(), do: %EQueue{} |
| @@ -30,9 +30,9 @@ defmodule EQueue do | |
| 30 30 | @doc """ |
| 31 31 | Calculates and returns the length of given queue |
| 32 32 | |
| 33 | - == Example |
| 34 | - iex> EQueue.from_list([:a, :b, :c]) |> EQueue.length |
| 35 | - 3 |
| 33 | + ## Example |
| 34 | + iex> EQueue.from_list([:a, :b, :c]) |> EQueue.length |
| 35 | + 3 |
| 36 36 | """ |
| 37 37 | @spec length(EQueue.t) :: pos_integer() |
| 38 38 | def length(%EQueue{data: queue}), do: :queue.len(queue) |
| @@ -41,25 +41,25 @@ defmodule EQueue do | |
| 41 41 | @doc """ |
| 42 42 | Adds an item to the end of the queue, returns the resulting queue |
| 43 43 | |
| 44 | - == Example |
| 45 | - iex> EQueue.new |> EQueue.push(:a) |
| 46 | - #EQueue<[:a]> |
| 44 | + ## Example |
| 45 | + iex> EQueue.new |> EQueue.push(:a) |
| 46 | + #EQueue<[:a]> |
| 47 47 | """ |
| 48 48 | @spec push(EQueue.t, any) :: EQueue.t |
| 49 49 | def push(%EQueue{data: queue}, item), do: :queue.in(item, queue) |> wrap |
| 50 50 | |
| 51 51 | |
| 52 52 | @doc """ |
| 53 | - Removes the item at the front of queue. Returns the tuple {:value, item, Q2}, |
| 53 | + Removes the item at the front of queue. Returns the tuple `{:value, item, Q2}`, |
| 54 54 | where item is the item removed and Q2 is the resulting queue. If Q1 is empty, |
| 55 | - the tuple {:empty, Q1} is returned. |
| 55 | + the tuple `{:empty, Q1}` is returned. |
| 56 56 | |
| 57 | - == Example |
| 58 | - iex> EQueue.from_list([:a, :b]) |> EQueue.pop |
| 59 | - {:value, :a, %EQueue{data: {[], [:b]} }} |
| 57 | + ## Examples |
| 58 | + iex> EQueue.from_list([:a, :b]) |> EQueue.pop |
| 59 | + {:value, :a, %EQueue{data: {[], [:b]} }} |
| 60 60 | |
| 61 | - iex> EQueue.new |> EQueue.pop |
| 62 | - {:empty, EQueue.new} |
| 61 | + iex> EQueue.new |> EQueue.pop |
| 62 | + {:empty, EQueue.new} |
| 63 63 | """ |
| 64 64 | @spec pop(EQueue.t) :: {:value, any, EQueue.t} |
| 65 65 | | {:empty, EQueue.t} |
| @@ -75,9 +75,9 @@ defmodule EQueue do | |
| 75 75 | Returns a list of the items in the queue in the same order; |
| 76 76 | the front item of the queue will become the head of the list. |
| 77 77 | |
| 78 | - == Example |
| 79 | - iex> EQueue.from_list([1, 2, 3, 4, 5]) |> EQueue.to_list |
| 80 | - [1, 2, 3, 4, 5] |
| 78 | + ## Example |
| 79 | + iex> EQueue.from_list([1, 2, 3, 4, 5]) |> EQueue.to_list |
| 80 | + [1, 2, 3, 4, 5] |
| 81 81 | """ |
| 82 82 | @spec to_list(EQueue.t) :: [any] |
| 83 83 | def to_list(%EQueue{data: queue}), do: :queue.to_list(queue) |
| @@ -87,9 +87,9 @@ defmodule EQueue do | |
| 87 87 | Returns a queue containing the items in L in the same order; |
| 88 88 | the head item of the list will become the front item of the queue. |
| 89 89 | |
| 90 | - == Example |
| 91 | - iex> EQueue.from_list [1, 2, 3, 4, 5] |
| 92 | - #EQueue<[1, 2, 3, 4, 5]> |
| 90 | + ## Example |
| 91 | + iex> EQueue.from_list [1, 2, 3, 4, 5] |
| 92 | + #EQueue<[1, 2, 3, 4, 5]> |
| 93 93 | """ |
| 94 94 | @spec from_list([any]) :: EQueue.t |
| 95 95 | def from_list(list), do: :queue.from_list(list) |> wrap |
| @@ -98,9 +98,9 @@ defmodule EQueue do | |
| 98 98 | @doc """ |
| 99 99 | Returns a new queue with the items for the given queue in reverse order |
| 100 100 | |
| 101 | - == Example |
| 102 | - iex> EQueue.from_list([1, 2, 3, 4, 5]) |> EQueue.reverse |
| 103 | - #EQueue<[5, 4, 3, 2, 1]> |
| 101 | + ## Example |
| 102 | + iex> EQueue.from_list([1, 2, 3, 4, 5]) |> EQueue.reverse |
| 103 | + #EQueue<[5, 4, 3, 2, 1]> |
| 104 104 | """ |
| 105 105 | @spec reverse(EQueue.t) :: EQueue.t |
| 106 106 | def reverse(%EQueue{data: queue}), do: :queue.reverse(queue) |> wrap |
| @@ -111,12 +111,12 @@ defmodule EQueue do | |
| 111 111 | the amount given and Q2 holds the rest. If attempted to split an empty |
| 112 112 | queue or past the length an argument error is raised |
| 113 113 | |
| 114 | - == Example |
| 115 | - iex> EQueue.from_list([1, 2, 3, 4, 5]) |> EQueue.split(3) |
| 116 | - {EQueue.from_list([1,2,3]), EQueue.from_list([4,5])} |
| 114 | + ## Examples |
| 115 | + iex> EQueue.from_list([1, 2, 3, 4, 5]) |> EQueue.split(3) |
| 116 | + {EQueue.from_list([1,2,3]), EQueue.from_list([4,5])} |
| 117 117 | |
| 118 | - iex> EQueue.from_list([1, 2, 3, 4, 5]) |> EQueue.split(12) |
| 119 | - ** (ArgumentError) argument error |
| 118 | + iex> EQueue.from_list([1, 2, 3, 4, 5]) |> EQueue.split(12) |
| 119 | + ** (ArgumentError) argument error |
| 120 120 | """ |
| 121 121 | @spec split(EQueue.t, pos_integer()) :: {EQueue.t, EQueue.t} |
| 122 122 | def split(%EQueue{data: queue}, amount) do |
| @@ -129,9 +129,9 @@ defmodule EQueue do | |
| 129 129 | Given two queues, an new queue is returned with the second appended to |
| 130 130 | the end of the first queue given |
| 131 131 | |
| 132 | - == Example |
| 133 | - iex> EQueue.from_list([1]) |> EQueue.join(EQueue.from_list([2])) |
| 134 | - #EQueue<[1, 2]> |
| 132 | + ## Example |
| 133 | + iex> EQueue.from_list([1]) |> EQueue.join(EQueue.from_list([2])) |
| 134 | + #EQueue<[1, 2]> |
| 135 135 | """ |
| 136 136 | @spec join(EQueue.t, EQueue.t) :: EQueue.t |
| 137 137 | def join(%EQueue{data: front}, %EQueue{data: back}) do |
| @@ -143,9 +143,9 @@ defmodule EQueue do | |
| 143 143 | With a given queue and function, a new queue is returned in the same |
| 144 144 | order as the one given where the function returns true for an element |
| 145 145 | |
| 146 | - == Example |
| 147 | - iex> EQueue.from_list([1, 2, 3, 4, 5]) |> EQueue.filter(fn x -> rem(x, 2) == 0 end) |
| 148 | - #EQueue<[2, 4]> |
| 146 | + ## Example |
| 147 | + iex> EQueue.from_list([1, 2, 3, 4, 5]) |> EQueue.filter(fn x -> rem(x, 2) == 0 end) |
| 148 | + #EQueue<[2, 4]> |
| 149 149 | """ |
| 150 150 | @spec filter(EQueue.t, Fun) :: EQueue.t |
| 151 151 | def filter(%EQueue{data: queue}, fun), do: :queue.filter(fun, queue) |> wrap |
| @@ -154,12 +154,12 @@ defmodule EQueue do | |
| 154 154 | @doc """ |
| 155 155 | Returns true if the given element is in the queue, false otherwise |
| 156 156 | |
| 157 | - == Example |
| 158 | - iex> EQueue.from_list([1, 2, 3]) |> EQueue.member? 2 |
| 159 | - true |
| 157 | + ## Examples |
| 158 | + iex> EQueue.from_list([1, 2, 3]) |> EQueue.member? 2 |
| 159 | + true |
| 160 160 | |
| 161 | - iex> EQueue.from_list([1, 2, 3]) |> EQueue.member? 9 |
| 162 | - false |
| 161 | + iex> EQueue.from_list([1, 2, 3]) |> EQueue.member? 9 |
| 162 | + false |
| 163 163 | """ |
| 164 164 | @spec member?(EQueue.t, any) :: true | false |
| 165 165 | def member?(%EQueue{data: queue}, item), do: :queue.member(item, queue) |
| @@ -168,12 +168,12 @@ defmodule EQueue do | |
| 168 168 | @doc """ |
| 169 169 | Returns true if the given queue is empty, false otherwise |
| 170 170 | |
| 171 | - == Example |
| 172 | - iex> EQueue.from_list([1, 2, 3]) |> EQueue.empty? |
| 173 | - false |
| 171 | + ## Examples |
| 172 | + iex> EQueue.from_list([1, 2, 3]) |> EQueue.empty? |
| 173 | + false |
| 174 174 | |
| 175 | - iex> EQueue.new |> EQueue.empty? |
| 176 | - true |
| 175 | + iex> EQueue.new |> EQueue.empty? |
| 176 | + true |
| 177 177 | """ |
| 178 178 | @spec empty?(EQueue.t) :: true | false |
| 179 179 | def empty?(%EQueue{data: queue}), do: :queue.is_empty(queue) |
| @@ -182,11 +182,12 @@ defmodule EQueue do | |
| 182 182 | @doc """ |
| 183 183 | Returns true if the given item is a queue, false otherwise |
| 184 184 | |
| 185 | - iex> EQueue.new |> EQueue.is_queue? |
| 186 | - true |
| 185 | + ## Examples |
| 186 | + iex> EQueue.new |> EQueue.is_queue? |
| 187 | + true |
| 187 188 | |
| 188 | - iex> {:a_queue?, [], []} |> EQueue.is_queue? |
| 189 | - false |
| 189 | + iex> {:a_queue?, [], []} |> EQueue.is_queue? |
| 190 | + false |
| 190 191 | """ |
| 191 192 | @spec is_queue?(any) :: true | false |
| 192 193 | def is_queue?(%EQueue{data: queue}), do: :queue.is_queue(queue) |
| @@ -3,7 +3,7 @@ defmodule EQueue.Mixfile do | |
| 3 3 | |
| 4 4 | def project do |
| 5 5 | [app: :e_queue, |
| 6 | - version: "1.0.0", |
| 6 | + version: "1.0.1", |
| 7 7 | elixir: "~> 1.1", |
| 8 8 | description: description, |
| 9 9 | package: package, |
| @@ -29,7 +29,10 @@ defmodule EQueue.Mixfile do | |
| 29 29 | # |
| 30 30 | # Type "mix help deps" for more examples and options |
| 31 31 | defp deps do |
| 32 | - [] |
| 32 | + [ |
| 33 | + {:earmark, "~> 0.1", only: :dev}, |
| 34 | + {:ex_doc, "~> 0.11", only: :dev} |
| 35 | + ] |
| 33 36 | end |
| 34 37 | |
| 35 38 | defp description do |