Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix toEnum for (Maybe a) #20

Merged
merged 2 commits into from
Nov 10, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Data/Enum.purs
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ instance boundedEnumMaybe :: BoundedEnum a => BoundedEnum (Maybe a) where
toEnum = to cardinality
where
to :: Cardinality a -> Int -> Maybe (Maybe a)
to _ 0 = Nothing
to (Cardinality ca) n | n <= ca = Just $ toEnum (n - 1)
to _ 0 = Just Nothing
to (Cardinality ca) n | n > 0 && n <= ca = Just $ toEnum (n - 1)
Copy link
Contributor

@paf31 paf31 Nov 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we even doing a check here? Why not just let the call to toEnum fail?

toEnum 0 = pure Nothing
toEnum n = Just <$> toEnum (n - 1)

to _ _ = Nothing
fromEnum Nothing = 0
fromEnum (Just e) = fromEnum e + 1
Expand Down
9 changes: 8 additions & 1 deletion test/Test/Data/Enum.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Control.Monad.Eff.Console (CONSOLE, log)

import Data.Enum (class Enum, class BoundedEnum, defaultToEnum, defaultFromEnum,
defaultCardinality, enumFromTo, enumFromThenTo, upFrom,
downFrom)
downFrom, toEnum)
import Data.Maybe (Maybe(..))

import Test.Assert (ASSERT, assert)
Expand Down Expand Up @@ -63,3 +63,10 @@ testEnum = do
assert $ downFrom D == [C, B, A]
assert $ downFrom B == [ A]
assert $ downFrom A == [ ]

log "BoundedEnum (Maybe Boolean)"
assert $ toEnum (-1) == Nothing :: Maybe (Maybe Boolean)
assert $ toEnum 0 == Just Nothing :: Maybe (Maybe Boolean)
assert $ toEnum 1 == Just (Just false) :: Maybe (Maybe Boolean)
assert $ toEnum 2 == Just (Just true) :: Maybe (Maybe Boolean)
assert $ toEnum 3 == Nothing :: Maybe (Maybe Boolean)