Skip to content

Instantly share code, notes, and snippets.

@chewbranca
Last active December 19, 2015 04:28
Show Gist options
  • Save chewbranca/5896818 to your computer and use it in GitHub Desktop.
Save chewbranca/5896818 to your computer and use it in GitHub Desktop.
% from: http://learnyousomeerlang.com/static/erlang/fifo_types.erl
%
-module(fifo).
-export([new/0, push/2, pop/1, empty/1]).
-export([test/0]).
-spec new() -> {fifo, [], []}.
new() -> {fifo, [], []}.
-spec push({fifo, In::list(), Out::list()}, term()) -> {fifo, list(), list()}.
push({fifo, In, Out}, X) -> {fifo, [X|In], Out}.
-spec pop({fifo, In::list(), Out::list()}) -> {term(), {fifo, list(), list()}}.
pop({fifo, [], []}) -> erlang:error('empty fifo');
pop({fifo, In, []}) -> pop({fifo, [], lists:reverse(In)});
pop({fifo, In, [H|T]}) -> {H, {fifo, In, T}}.
-spec empty({fifo, [], []}) -> true;
({fifo, nonempty_list(), nonempty_list()}) -> false.
empty({fifo, [], []}) -> true;
empty({fifo, _, _}) -> false.
test() ->
N = new(),
{2, N2} = pop(push(push(new(), 2), 5)),
{5, N3} = pop(N2),
N = N3,
true = empty(N3),
false = empty(N2),
pop({fifo, [a, b], [e]}),
F = push(new(), asdf), % F = {fifo,[asdf],[]}
false = empty(F).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment