From ce9a84f07bc4ee5124c87109729f1ccb2777851f Mon Sep 17 00:00:00 2001 From: Fred Yankowski Date: Wed, 27 Apr 2022 09:24:44 -0500 Subject: [PATCH 1/2] add new `length` function for fast calculation of Fifo length --- src/Fifo.elm | 42 +++++++++++++++++++++++++++++++----------- tests/Tests.elm | 18 ++++++++++++++++++ 2 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/Fifo.elm b/src/Fifo.elm index 9ece297..15c7cbb 100644 --- a/src/Fifo.elm +++ b/src/Fifo.elm @@ -2,6 +2,7 @@ module Fifo exposing ( Fifo, empty, fromList , insert, remove , toList + , length ) {-| @@ -21,13 +22,18 @@ module Fifo exposing @docs toList + +# Inspecting + +@docs length + -} {-| A FIFO containing items of type `a`. -} type Fifo a - = Fifo (List a) (List a) + = Fifo (List a) (List a) Int {-| Creates an empty Fifo. @@ -38,7 +44,7 @@ type Fifo a -} empty : Fifo a empty = - Fifo [] [] + Fifo [] [] 0 {-| Inserts an item into a Fifo @@ -50,8 +56,8 @@ empty = -} insert : a -> Fifo a -> Fifo a -insert a (Fifo front back) = - Fifo front (a :: back) +insert a (Fifo front back len) = + Fifo front (a :: back) (len + 1) {-| Removes the next (oldest) item from a Fifo, returning the item (if any), and the updated Fifo. @@ -64,14 +70,14 @@ insert a (Fifo front back) = remove : Fifo a -> ( Maybe a, Fifo a ) remove fifo = case fifo of - Fifo [] [] -> + Fifo [] [] _ -> ( Nothing, empty ) - Fifo [] back -> - remove <| Fifo (List.reverse back) [] + Fifo [] back len -> + remove <| Fifo (List.reverse back) [] len - Fifo (next :: rest) back -> - ( Just next, Fifo rest back ) + Fifo (next :: rest) back len -> + ( Just next, Fifo rest back (len - 1) ) {-| Creates a Fifo from a List. @@ -84,7 +90,7 @@ remove fifo = -} fromList : List a -> Fifo a fromList list = - Fifo list [] + Fifo list [] (List.length list) {-| Converts a Fifo to a List. @@ -97,5 +103,19 @@ fromList list = -} toList : Fifo a -> List a -toList (Fifo front back) = +toList (Fifo front back _) = front ++ List.reverse back + + +{-| Returns the number of items in the Fifo. + + Fifo.fromList [1, 2, 3] + |> Fifo.remove + |> Tuple.second + |> Fifo.length + -- == 2 + +-} +length : Fifo a -> Int +length (Fifo _ _ len) = + len diff --git a/tests/Tests.elm b/tests/Tests.elm index 3220ce4..d1d6ddf 100644 --- a/tests/Tests.elm +++ b/tests/Tests.elm @@ -83,4 +83,22 @@ all = |> Fifo.remove |> Tuple.first |> Expect.equal (Just 2) + , test "length" <| + \() -> + Fifo.fromList [ 1, 2, 3 ] + |> Fifo.length + |> Expect.equal 3 + , test "length empty" <| + \() -> + Fifo.empty + |> Fifo.length + |> Expect.equal 0 + + , test "length remove" <| + \() -> + Fifo.fromList [ 1, 2, 3 ] + |> Fifo.remove + |> Tuple.second + |> Fifo.length + |> Expect.equal 2 ] From f5fab70bf75815b4666f59df88c22d0f08e858b9 Mon Sep 17 00:00:00 2001 From: Fred Yankowski Date: Wed, 27 Apr 2022 09:48:42 -0500 Subject: [PATCH 2/2] test length after insert --- tests/Tests.elm | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/Tests.elm b/tests/Tests.elm index d1d6ddf..a7dba83 100644 --- a/tests/Tests.elm +++ b/tests/Tests.elm @@ -93,7 +93,13 @@ all = Fifo.empty |> Fifo.length |> Expect.equal 0 - + , test "length insert" <| + \() -> + Fifo.empty + |> Fifo.insert 42 + |> Fifo.insert 60 + |> Fifo.length + |> Expect.equal 2 , test "length remove" <| \() -> Fifo.fromList [ 1, 2, 3 ]