From afcd070b284af7cda2b727aa6d7dd464a754be51 Mon Sep 17 00:00:00 2001 From: Georgy Sychev Date: Tue, 17 Oct 2023 02:26:54 +0400 Subject: [PATCH] to_list/1 --- lib/persistent_queue.ex | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/persistent_queue.ex b/lib/persistent_queue.ex index ed8d882..c224625 100644 --- a/lib/persistent_queue.ex +++ b/lib/persistent_queue.ex @@ -159,4 +159,25 @@ defmodule PersistentQueue do {{:value, entry}, %{queue | ins: ins, outs: outs, size: size - 1, storage: storage}} end end + + @doc """ + Converts queue to list, where the first entry is the head entry + + ## Example: + + iex> queue = new(limit: 4, storage: %PersistentQueue.DroppingStorage{}) + iex> queue = + ...> queue + ...> |> enqueue(1) + ...> |> enqueue(2) + ...> |> enqueue(3) + iex> to_list(queue) + [1, 2, 3] + """ + @spec to_list(t(entry)) :: [entry] + when entry: term() + def to_list(queue) do + {entries, _} = dequeue_n(queue, queue.size) + entries + end end