From 3d705c4e6dfb46ac9acf27025b34c628269381ac Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Wed, 18 Sep 2019 02:16:10 -0400 Subject: [PATCH] implemented nth --- src/Mtlstats/Util.hs | 29 +++++++++++++++++++++++++++++ test/Spec.hs | 2 ++ test/UtilSpec.hs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 src/Mtlstats/Util.hs create mode 100644 test/UtilSpec.hs diff --git a/src/Mtlstats/Util.hs b/src/Mtlstats/Util.hs new file mode 100644 index 0000000..3d8880f --- /dev/null +++ b/src/Mtlstats/Util.hs @@ -0,0 +1,29 @@ +{- | + +mtlstats +Copyright (C) 2019 Rhéal Lamothe + + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or (at +your option) any later version. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +-} + +module Mtlstats.Util (nth) where + +nth :: Int -> [a] -> Maybe a +nth _ [] = Nothing +nth n (x:xs) + | n == 0 = Just x + | n < 0 = Nothing + | otherwise = nth (pred n) xs diff --git a/test/Spec.hs b/test/Spec.hs index a3db0ba..4edf493 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -26,6 +26,7 @@ import qualified FormatSpec as Format import qualified HandlersSpec as Handlers import qualified ReportSpec as Report import qualified TypesSpec as Types +import qualified UtilSpec as Util main :: IO () main = hspec $ do @@ -34,3 +35,4 @@ main = hspec $ do Format.spec Handlers.spec Report.spec + Util.spec diff --git a/test/UtilSpec.hs b/test/UtilSpec.hs new file mode 100644 index 0000000..e5e1f37 --- /dev/null +++ b/test/UtilSpec.hs @@ -0,0 +1,44 @@ +{- + +mtlstats +Copyright (C) 2019 Rhéal Lamothe + + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or (at +your option) any later version. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +-} + +module UtilSpec (spec) where + +import Test.Hspec (Spec, context, describe, it, shouldBe) + +import Mtlstats.Util + +spec :: Spec +spec = describe "Mtlstats.Util" + nthSpec + +nthSpec :: Spec +nthSpec = describe "nth" $ mapM_ + (\(n, expected) -> context (show n) $ + it ("should be " ++ show expected) $ let + xs = ["foo", "bar", "baz"] + in nth n xs `shouldBe` expected) + -- index, expected + [ ( 0, Just "foo" ) + , ( 1, Just "bar" ) + , ( 2, Just "baz" ) + , ( 3, Nothing ) + , ( -1, Nothing ) + ]