implemented nth
This commit is contained in:
parent
ed9e437a1a
commit
3d705c4e6d
|
@ -0,0 +1,29 @@
|
|||
{- |
|
||||
|
||||
mtlstats
|
||||
Copyright (C) 2019 Rhéal Lamothe
|
||||
<rheal.lamothe@gmail.com>
|
||||
|
||||
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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
-}
|
||||
|
||||
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
|
|
@ -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
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
{-
|
||||
|
||||
mtlstats
|
||||
Copyright (C) 2019 Rhéal Lamothe
|
||||
<rheal.lamothe@gmail.com>
|
||||
|
||||
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 <https://www.gnu.org/licenses/>.
|
||||
|
||||
-}
|
||||
|
||||
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 )
|
||||
]
|
Loading…
Reference in New Issue
Block a user