Skip to content

Commit 7ccd33d

Browse files
committed
Fix enumFromTo, closes #15
1 parent 69fdb80 commit 7ccd33d

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
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

0 commit comments

Comments
 (0)