diff --git a/package.json b/package.json index bf77170..f1deb35 100644 --- a/package.json +++ b/package.json @@ -6,8 +6,8 @@ "test": "pulp test" }, "devDependencies": { - "pulp": "^10.0.4", - "purescript-psa": "^0.5.0-rc.1", + "pulp": "^11.0.0", + "purescript-psa": "^0.5.1", "rimraf": "^2.6.1" } } diff --git a/src/Data/Enum.purs b/src/Data/Enum.purs index 7cd99f0..ecf1281 100644 --- a/src/Data/Enum.purs +++ b/src/Data/Enum.purs @@ -5,6 +5,7 @@ module Data.Enum , enumFromTo , enumFromThenTo , upFrom + , upFromIncluding , downFrom , Cardinality(..) , class BoundedEnum, cardinality, toEnum, fromEnum, toEnumWithDefaults @@ -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) @@ -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) diff --git a/test/Test/Data/Enum.purs b/test/Test/Data/Enum.purs index d25839a..b6a7c7f 100644 --- a/test/Test/Data/Enum.purs +++ b/test/Test/Data/Enum.purs @@ -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) @@ -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] @@ -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 -