Skip to content

Commit

Permalink
Merge pull request #31 from safareli/upFromIncluding
Browse files Browse the repository at this point in the history
define upFromIncluding
  • Loading branch information
paf31 authored Jun 11, 2017
2 parents d307db5 + e5e2fcf commit fbdce5e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
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 fbdce5e

Please sign in to comment.