diff --git a/src/Abacus.hs b/src/Abacus.hs index 49e22e1..bad1d70 100644 --- a/src/Abacus.hs +++ b/src/Abacus.hs @@ -24,7 +24,15 @@ License along with this program. If not, see -} -module Abacus (Abacus, newAbacus, getNumBeads, getNumRungs) where +module Abacus ( + Abacus, + newAbacus, + getNumBeads, + getNumRungs, + getRung, + ) where + +import Data.List (find) import Abacus.Internal @@ -48,4 +56,16 @@ getNumBeads = abacusNumBeads getNumRungs :: Abacus -> Int getNumRungs = length . abacusRungs +-- | Returns the number of beads slid across a given rung +getRung + :: Int + -- ^ The rung number + -> Abacus + -- ^ Abacus being checked + -> Maybe Int + -- ^ The number of beads slid across (if the rung exists) +getRung r a = snd <$> find + (\(n, _) -> n == r) + (zip [0..] $ abacusRungs a) + --jl diff --git a/test/AbacusSpec.hs b/test/AbacusSpec.hs index 1975c47..2240b68 100644 --- a/test/AbacusSpec.hs +++ b/test/AbacusSpec.hs @@ -32,6 +32,7 @@ spec = describe "Abacus" $ do newAbacusSpec getNumBeadsSpec getNumRungsSpec + getRungSpec newAbacusSpec :: Spec newAbacusSpec = describe "newAbacusSpec" $ mapM_ @@ -73,4 +74,18 @@ getNumRungsSpec = describe "getNumRungs" $ mapM_ tenRungs = build 10 build n = fromJust $ newAbacus 10 n +getRungSpec :: Spec +getRungSpec = describe "getRung" $ mapM_ + ( \(desc, rung, expected) -> context desc $ let + actual = getRung rung abacus + in it ("should be " ++ show expected) $ + actual `shouldBe` expected + ) + [ ( "0th rung", 0, Just 2 ) + , ( "last rung", 2, Just 5 ) + , ( "negative rung", -1, Nothing ) + , ( "too large", 3, Nothing ) + ] where + abacus = Abacus 10 [2, 3, 5] + --jl