Skip to content

Commit

Permalink
Merge pull request #21 from LiamGoodacre/fix/simplify-bounded-either
Browse files Browse the repository at this point in the history
Simplify toEnum for Either instance.
  • Loading branch information
garyb authored Nov 10, 2016
2 parents dc6bbed + 73e6c20 commit 3b6c084
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/Data/Enum.purs
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,12 @@ instance boundedEnumEither :: (BoundedEnum a, BoundedEnum b) => BoundedEnum (Eit
Cardinality
$ unwrap (cardinality :: Cardinality a)
+ unwrap (cardinality :: Cardinality b)
toEnum n = to cardinality cardinality
toEnum n = to cardinality
where
to :: Cardinality a -> Cardinality (Either a b) -> Maybe (Either a b)
to (Cardinality ca) (Cardinality cab)
to :: Cardinality a -> Maybe (Either a b)
to (Cardinality ca)
| n >= 0 && n < ca = Left <$> toEnum n
| n >= ca && n < cab = Right <$> toEnum (n - ca)
| otherwise = Nothing
| otherwise = Right <$> toEnum (n - ca)
fromEnum (Left a) = fromEnum a
fromEnum (Right b) = fromEnum b + unwrap (cardinality :: Cardinality a)

Expand Down

0 comments on commit 3b6c084

Please sign in to comment.