Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 75 additions & 14 deletions src/Fifo.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Fifo (Fifo, empty, insert, remove, fromList, toList) where
module Fifo (Fifo, empty, isEmpty, insert, remove, front, length, fromList, toList) where

{-|

Expand All @@ -11,14 +11,16 @@ module Fifo (Fifo, empty, insert, remove, fromList, toList) where
# To List
@docs toList

# Checking state
@docs isEmpty, front, length

-}


{-| A FIFO containing items of type `a`.
-}
type Fifo a
= Fifo (List a) (List a)

= Empty | Node a (List a)

{-| Creates an empty Fifo.

Expand All @@ -28,8 +30,19 @@ type Fifo a
-}
empty : Fifo a
empty =
Fifo [] []
Empty


{-| Checks whether a Fifo is empty.

Fifo.empty
|> Fifo.insert 7
|> Fifo.isEmpty
-- == False
-}
isEmpty : Fifo a -> Bool
isEmpty fifo =
fifo == Empty

{-| Inserts an item into a Fifo

Expand All @@ -40,8 +53,13 @@ empty =

-}
insert : a -> Fifo a -> Fifo a
insert a (Fifo front back) =
Fifo front (a :: back)
insert a fifo =
case fifo of
Empty ->
Node a []

Node front back ->
Node front (List.append back [a])


{-| Removes the next (oldest) item from a Fifo, returning the item (if any), and the updated Fifo.
Expand All @@ -54,14 +72,47 @@ insert a (Fifo front back) =
remove : Fifo a -> ( Maybe a, Fifo a )
remove fifo =
case fifo of
Fifo [] [] ->
Empty ->
( Nothing, empty )

Fifo [] back ->
remove <| Fifo (List.reverse back) []
Node front [] ->
( Just front, empty )

Fifo (next :: rest) back ->
( Just next, Fifo rest back )
Node front (next :: rest) ->
( Just front, Node next rest )


{-| Reads a Fifo's front value.

Fifo.fromList [3,7]
|> Fifo.front
-- == (Just 3)

-}
front : Fifo a -> Maybe a
front fifo =
case fifo of
Empty ->
Nothing

Node front back ->
Just front


{-| Reports the length of a Fifo.

Fifo.fromList [3,4,5]
|> Fifo.length
-- == 3

-}
length : Fifo a -> Int
length fifo =
case fifo of
Empty ->
0
Node front back ->
1 + List.length back


{-| Creates a Fifo from a List.
Expand All @@ -74,7 +125,12 @@ remove fifo =
-}
fromList : List a -> Fifo a
fromList list =
Fifo list []
case list of
(first :: rest) ->
Node first rest

[] ->
empty


{-| Converts a Fifo to a List.
Expand All @@ -86,5 +142,10 @@ fromList list =
-- == [7,9]
-}
toList : Fifo a -> List a
toList (Fifo front back) =
front ++ List.reverse back
toList fifo =
case fifo of
Empty ->
[]

Node front back ->
[front] ++ back
31 changes: 29 additions & 2 deletions tests/Tests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@ all =
|> assertEqual Nothing
|> test "empty FIFO contains nothing",

Fifo.empty
|> Fifo.isEmpty
|> assertEqual True
|> test "empty FIFO is reported as such",

Fifo.empty
|> Fifo.insert 7
|> Fifo.isEmpty
|> assertEqual False
|> test "non-empty FIFO is reported as such",

Fifo.empty
|> Fifo.insert 7
|> Fifo.insert 8
|> Fifo.front
|> assertEqual (Just 7)
|> test "can read the front value",

Fifo.empty
|> Fifo.insert 9
|> Fifo.remove |> fst
Expand Down Expand Up @@ -43,7 +61,7 @@ all =
|> Fifo.insert 7
|> Fifo.remove |> fst
|> assertEqual (Just 8)
|> test "removing an item after inserting after a removal",
|> test "can remove an item after removal followed by insertion",

Fifo.empty
|> Fifo.insert 9
Expand All @@ -53,7 +71,16 @@ all =
|> Fifo.remove |> snd
|> Fifo.remove |> fst
|> assertEqual (Just 7)
|> test "removing an item inserted after a removal",
|> test "can remove an item inserted after a removal",

Fifo.empty
|> Fifo.insert 9
|> Fifo.insert 8
|> Fifo.remove |> snd
|> Fifo.insert 7
|> Fifo.length
|> assertEqual 2
|> test "keeping track of length",

Fifo.empty
|> Fifo.insert 9
Expand Down
4 changes: 2 additions & 2 deletions tests/elm-package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
],
"exposed-modules": [],
"dependencies": {
"avh4/elm-test": "1.0.2 <= v < 2.0.0",
"deadfoxygrandpa/elm-test": "1.0.2 <= v < 2.0.0",
"elm-lang/core": "2.1.0 <= v < 3.0.0",
"laszlopandy/elm-console": "1.0.2 <= v < 2.0.0"
},
"elm-version": "0.16.0 <= v < 0.17.0"
}
}