Skip to content

Commit

Permalink
define upFromIncluding
Browse files Browse the repository at this point in the history
  • Loading branch information
safareli committed Jun 10, 2017
1 parent d6e3b2d commit e5e2fcf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/Data/Enum.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Data.Enum
, enumFromTo
, enumFromThenTo
, upFrom
, upFromIncluding
, downFrom
, Cardinality(..)
, class BoundedEnum, cardinality, toEnum, fromEnum, toEnumWithDefaults
Expand All @@ -21,6 +22,7 @@ import Data.Char (fromCharCode, toCharCode)
import Data.Either (Either(..))
import Data.Maybe (Maybe(..), maybe, fromJust)
import Data.Newtype (class Newtype, unwrap)
import Data.NonEmpty (NonEmpty, (:|))
import Data.Tuple (Tuple(..))
import Data.Unfoldable (class Unfoldable, unfoldr)

Expand Down Expand Up @@ -129,9 +131,16 @@ intStepFromTo step from to =
diag :: forall a. a -> Tuple a a
diag a = Tuple a a

-- | Results in all successors from given Enum in some unfoldable.
-- | Note that given Enum is not included in the result.
upFrom :: forall a u. Enum a => Unfoldable u => a -> u a
upFrom = unfoldr (map diag <<< succ)

-- | Results in all successors of given Enum (including itself)
-- | in some NonEmpty unfoldable.
upFromIncluding :: a u. Enum a => Unfoldable u => a -> NonEmpty u a
upFromIncluding x = x :| upFrom x

downFrom :: forall a u. Enum a => Unfoldable u => a -> u a
downFrom = unfoldr (map diag <<< pred)

Expand Down
9 changes: 7 additions & 2 deletions test/Test/Data/Enum.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import Control.Monad.Eff.Console (CONSOLE, log)

import Data.Newtype (unwrap)
import Data.Enum (class Enum, class BoundedEnum, defaultToEnum, defaultFromEnum,
defaultCardinality, enumFromTo, enumFromThenTo, upFrom,
defaultCardinality, enumFromTo, enumFromThenTo, upFrom, upFromIncluding,
downFrom, toEnum, fromEnum, Cardinality, cardinality)
import Data.Maybe (Maybe(..))
import Data.NonEmpty ((:|))
import Data.Either (Either(..))

import Test.Assert (ASSERT, assert)
Expand Down Expand Up @@ -61,6 +62,11 @@ testEnum = do
assert $ upFrom D == [ E]
assert $ upFrom E == [ ]

log "upFromIncluding"
assert $ upFromIncluding B == B :| [C, D, E]
assert $ upFromIncluding D == D :| [ E]
assert $ upFromIncluding E == E :| [ ]

log "downFrom"
assert $ downFrom D == [C, B, A]
assert $ downFrom B == [ A]
Expand All @@ -81,4 +87,3 @@ testEnum = do
assert $ fromEnum (Left false :: Either Boolean T) == 0
assert $ fromEnum (Left true :: Either Boolean T) == 1
assert $ fromEnum (Right B :: Either Boolean T) == 3

0 comments on commit e5e2fcf

Please sign in to comment.