Giter Site home page Giter Site logo

fi_fo's Introduction

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]}}

fi_fo's People

Contributors

nickneck avatar marcus-kruse-gcx avatar

Stargazers

 avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.