Skip to content

hrzndhrn/fi_fo

Repository files navigation

FiFo

Hex.pm: version GitHub: CI status Coveralls: coverage License: MIT

This module provides (double-ended) FIFO queues in an efficient manner.

FiFo is just a rewrite of the Erlang module queue in an Elixir way.

Installation

First, add fi_fo to your mix.exs dependencies:

def deps do
  [{:fi_fo, "~> 0.2"}]
end

Then, update your dependencies:

$ mix deps.get

Documentation can be found at HexDocs.

Usage

Construct, write, and read: FiFo.new/0, FiFo.put/2, and FiFo.get/1:

iex(1)> queue = FiFo.new()
{[], []}
iex(2)> queue = FiFo.put(queue, 1)
{[1], []}
iex(3)> queue = FiFo.push(queue, 2)
{[2], [1]}
iex(4)> {1, queue} = FiFo.get(queue)
{1, {[2], []}}
iex(5)> {2, queue} = FiFo.get(queue)
{2, {[], []}}
iex(6)> FiFo.get(queue)
{nil, {[], []}}
iex(7)> FiFo.get(queue, :empty)
{:empty, {[], []}}

Create a queue from other data structures: FiFo.new/1

iex(7)> FiFo.new([1, 2, 3, 4])
{[4, 3], [1, 2]}
iex(8)> FiFo.new(1..10)
{[10, 9, 8, 7, 6], [1, 2, 3, 4, 5]}

Convert a queue to a list: FiFo.to_list/1

iex(9)> FiFo.new([1..3]) |> FiFo.to_list()
[1, 2, 3]

Take and drop elements: FiFo.drop/2 and FiFo.take/2

iex(10)> FiFo.new(1..10)
{[10, 9, 8, 7, 6], [1, 2, 3, 4, 5]}
iex(11)> FiFo.drop(queue, 3)
{[10, 9, 8, 7, 6], [4, 5]}
iex(12)> FiFo.drop(queue, -6)
{[4, 3], [1, 2]}
iex(13)> FiFo.take(queue, 3)
{[1, 2, 3], {[10, 9, 8, 7, 6], [4, 5]}}
iex(12)> FiFo.take(queue, -6)
{[10, 9, 8, 7, 6, 5], {[4, 3], [1, 2]}}

Releases

No releases published

Packages

No packages published

Languages