-
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.
- Loading branch information
1 parent
20f1c88
commit aa6e267
Showing
2 changed files
with
75 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
defmodule Buildel.Blocks.Utils.InputQueue do | ||
defstruct [:queue, :process_item] | ||
alias __MODULE__ | ||
|
||
@type item :: any() | ||
@type queue :: [item()] | ||
@type process_item :: (item() -> any()) | ||
@type t :: %InputQueue{queue: queue(), process_item: process_item()} | ||
|
||
@spec new(queue(), process_item()) :: t() | ||
def new(items, process_item) do | ||
%__MODULE__{queue: items, process_item: process_item} | ||
end | ||
|
||
@spec push(t(), item()) :: t() | ||
def push(%__MODULE__{queue: [], process_item: process_item}, item) do | ||
IO.puts("pushing first item") | ||
process_item.(item) | ||
|
||
%__MODULE__{queue: [item], process_item: process_item} | ||
end | ||
|
||
def push(%__MODULE__{queue: queue, process_item: process_item}, item) do | ||
IO.puts("pushing another item") | ||
|
||
%__MODULE__{queue: queue ++ [item], process_item: process_item} | ||
end | ||
|
||
@spec pop(t()) :: t() | ||
def pop(%__MODULE__{queue: [], process_item: process_item}) do | ||
IO.puts("removing from empty queue") | ||
%__MODULE__{queue: [], process_item: process_item} | ||
end | ||
|
||
@spec pop(t()) :: t() | ||
def pop(%__MODULE__{queue: [_item], process_item: process_item}) do | ||
IO.puts("removing last item") | ||
%__MODULE__{queue: [], process_item: process_item} | ||
end | ||
|
||
def pop(%__MODULE__{queue: [_item | [item]], process_item: process_item}) do | ||
IO.puts("removing another item") | ||
process_item.(item) | ||
|
||
%__MODULE__{queue: [item], process_item: process_item} | ||
end | ||
|
||
def pop(%__MODULE__{queue: [_item | [item | rest]], process_item: process_item}) do | ||
IO.puts("removing another item from queue") | ||
process_item.(item) | ||
|
||
%__MODULE__{queue: rest, process_item: process_item} | ||
end | ||
end |