From 2cb279e7e7acc329e1f450289c7f2ebbeafb1b35 Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Thu, 12 Mar 2020 22:41:28 -0400 Subject: [PATCH] implemented dropNth --- src/Mtlstats/Util.hs | 13 +++++++++++++ test/UtilSpec.hs | 15 +++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/Mtlstats/Util.hs b/src/Mtlstats/Util.hs index 951859c..02249f9 100644 --- a/src/Mtlstats/Util.hs +++ b/src/Mtlstats/Util.hs @@ -22,6 +22,7 @@ along with this program. If not, see . module Mtlstats.Util ( nth , modifyNth + , dropNth , updateMap , slice , capitalizeName @@ -56,6 +57,18 @@ modifyNth n f = zipWith (\i x -> if i == n then f x else x) [0..] +-- | Attempt to drop the nth element from a list +dropNth + :: Int + -- ^ The index of the element to drop + -> [a] + -- ^ The list to be modified + -> [a] + -- ^ The modified list +dropNth n = foldr + (\(i, x) acc -> if i == n then acc else x : acc) + [] . zip [0..] + -- | Modify a value indexed by a given key in a map using a default -- initial value if not present updateMap diff --git a/test/UtilSpec.hs b/test/UtilSpec.hs index 13d9a39..b0c49f1 100644 --- a/test/UtilSpec.hs +++ b/test/UtilSpec.hs @@ -30,6 +30,7 @@ spec :: Spec spec = describe "Mtlstats.Util" $ do nthSpec modifyNthSpec + dropNthSpec updateMapSpec sliceSpec capitalizeNameSpec @@ -64,6 +65,20 @@ modifyNthSpec = describe "modifyNth" $ do it "should not modify the value" $ modifyNth (-1) succ list `shouldBe` [1, 2, 3] +dropNthSpec :: Spec +dropNthSpec = describe "dropNth" $ mapM_ + + (\(label, n, expected) -> + context label $ + it ("should be " ++ show expected) $ + dropNth n list `shouldBe` expected) + + [ ( "out of bounds", 1, ["foo", "baz"] ) + , ( "in bounds", 3, list ) + ] + + where list = ["foo", "bar", "baz"] + updateMapSpec :: Spec updateMapSpec = describe "updateMap" $ do let