implemented dropNth

This commit is contained in:
Jonathan Lamothe 2020-03-12 22:41:28 -04:00
parent 7ca66ad801
commit 2cb279e7e7
2 changed files with 28 additions and 0 deletions

View File

@ -22,6 +22,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
module Mtlstats.Util module Mtlstats.Util
( nth ( nth
, modifyNth , modifyNth
, dropNth
, updateMap , updateMap
, slice , slice
, capitalizeName , capitalizeName
@ -56,6 +57,18 @@ modifyNth n f = zipWith
(\i x -> if i == n then f x else x) (\i x -> if i == n then f x else x)
[0..] [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 -- | Modify a value indexed by a given key in a map using a default
-- initial value if not present -- initial value if not present
updateMap updateMap

View File

@ -30,6 +30,7 @@ spec :: Spec
spec = describe "Mtlstats.Util" $ do spec = describe "Mtlstats.Util" $ do
nthSpec nthSpec
modifyNthSpec modifyNthSpec
dropNthSpec
updateMapSpec updateMapSpec
sliceSpec sliceSpec
capitalizeNameSpec capitalizeNameSpec
@ -64,6 +65,20 @@ modifyNthSpec = describe "modifyNth" $ do
it "should not modify the value" $ it "should not modify the value" $
modifyNth (-1) succ list `shouldBe` [1, 2, 3] 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 :: Spec
updateMapSpec = describe "updateMap" $ do updateMapSpec = describe "updateMap" $ do
let let