Skip to content

Commit 49c7205

Browse files
committed
Fix enumFromTo, closes #15
1 parent 69fdb80 commit 49c7205

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/Data/Enum.purs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ module Data.Enum
1515

1616
import Prelude
1717

18+
import Control.MonadPlus (guard)
19+
1820
import Data.Char (fromCharCode, toCharCode)
1921
import Data.Either (Either(..))
2022
import Data.Maybe (Maybe(..), maybe, fromJust)
@@ -93,12 +95,15 @@ defaultSucc toEnum' fromEnum' a = toEnum' (fromEnum' a + 1)
9395
defaultPred :: forall a. (Int -> Maybe a) -> (a -> Int) -> a -> Maybe a
9496
defaultPred toEnum' fromEnum' a = toEnum' (fromEnum' a - 1)
9597

96-
-- | Property: ```fromEnum a = a', fromEnum b = b' => forall e', a' <= e' <= b': Exists e: toEnum e' = Just e```
97-
-- |
98-
-- | Following from the propery of `intFromTo`, we are sure all elements in `intFromTo (fromEnum a) (fromEnum b)` are `Just`s.
99-
-- TODO need to update the doc comment above
98+
-- | Returns a successive sequence of elements from the lower bound to
99+
-- | the upper bound (inclusive).
100100
enumFromTo :: forall a u. (Enum a, Unfoldable u) => a -> a -> u a
101-
enumFromTo from to = unfoldr (\x -> succ x >>= \x' -> if x <= to then pure $ Tuple x x' else Nothing) from
101+
enumFromTo from to = unfoldr go (Just from)
102+
where
103+
go mx = do
104+
x <- mx
105+
guard (x <= to)
106+
pure $ Tuple x (succ x)
102107

103108
-- | `[a,b..c]`
104109
enumFromThenTo :: forall a. BoundedEnum a => a -> a -> a -> Array a

test/Test/Data/Enum.purs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import Prelude
55
import Control.Monad.Eff (Eff)
66
import Control.Monad.Eff.Console (CONSOLE, log)
77

8-
import Data.Enum (class Enum, class BoundedEnum, Cardinality(..), defaultToEnum,
9-
defaultFromEnum, defaultCardinality, enumFromTo, enumFromThenTo,
10-
upFrom, downFrom)
8+
import Data.Enum (class Enum, class BoundedEnum, defaultToEnum, defaultFromEnum,
9+
defaultCardinality, enumFromTo, enumFromThenTo, upFrom,
10+
downFrom)
1111
import Data.Maybe (Maybe(..))
1212

1313
import Test.Assert (ASSERT, assert)

0 commit comments

Comments
 (0)