Skip to content

Enum.range - all elements of an enum, ordered #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@
"dependencies": {
"purescript-either": "^3.0.0",
"purescript-strings": "^3.0.0",
"purescript-unfoldable": "^3.0.0"
"purescript-unfoldable": "^3.0.0",
"purescript-arrays": "matthewleon/purescript-arrays#nonempty"
},
"devDependencies": {
"purescript-assert": "^3.0.0",
"purescript-console": "^3.0.0"
},
"resolutions": {
"purescript-arrays": "nonempty"
}
}
30 changes: 30 additions & 0 deletions src/Data/Enum/Range.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Data.Enum.Range
( Range
, range
, toNonEmptyArray
, cardinalityFromRange
, toEnumFromRange
) where

import Prelude

import Data.Array.NonEmpty (NonEmptyArray, fromNonEmpty, index, length)
import Data.Enum (class Enum, Cardinality(..), upFromIncluding)
import Data.Maybe (Maybe)

newtype Range a = Range (NonEmptyArray a)

-- smart constructor for Range
range :: forall a. Bounded a => Enum a => Range a
range = Range $ fromNonEmpty $ upFromIncluding bottom

toNonEmptyArray :: forall a. Range a -> NonEmptyArray a
toNonEmptyArray (Range xs) = xs

-- | Runs in `O(1)`
cardinalityFromRange :: forall a. Range a -> Cardinality a
cardinalityFromRange = Cardinality <<< length <<< toNonEmptyArray

-- | Runs in `O(1)`
toEnumFromRange :: forall a. Range a -> Int -> Maybe a
toEnumFromRange = index <<< toNonEmptyArray
5 changes: 4 additions & 1 deletion test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import Control.Monad.Eff.Console (CONSOLE)

import Test.Assert (ASSERT)
import Test.Data.Enum (testEnum)
import Test.Data.Enum.Range (testRange)

main :: Eff (console :: CONSOLE, assert :: ASSERT) Unit
main = testEnum
main = do
testEnum
testRange
56 changes: 56 additions & 0 deletions test/Test/Data/Enum/Range.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module Test.Data.Enum.Range (testRange) where

import Prelude

import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Data.Array.NonEmpty (toArray)
import Data.Enum (class Enum, class BoundedEnum, Cardinality(..), cardinality, defaultFromEnum, toEnum)
import Data.Enum.Range (Range, range, toNonEmptyArray, cardinalityFromRange, toEnumFromRange)
import Data.Maybe (Maybe(..))
import Test.Assert (ASSERT, assert)

data T = A | B | C | D | E

derive instance eqT :: Eq T
derive instance ordT :: Ord T

instance enumT :: Enum T where
succ A = Just B
succ B = Just C
succ C = Just D
succ D = Just E
succ E = Nothing

pred A = Nothing
pred B = Just A
pred C = Just B
pred D = Just C
pred E = Just D

instance boundedT :: Bounded T where
bottom = A
top = E

instance boundedEnumT :: BoundedEnum T where
cardinality = cardinalityFromRange rangeT
toEnum = toEnumFromRange rangeT
fromEnum = defaultFromEnum

rangeT :: Range T
rangeT = range

testRange :: Eff (console :: CONSOLE, assert :: ASSERT) Unit
testRange = do
log "toNonEmptyArray"
assert $ toArray (toNonEmptyArray rangeT) == [A, B, C, D, E]

log "cardinalityFromRange"
assert $ cardinality == (Cardinality 5 :: Cardinality T)

log "toEnumFromRange"
assert $ toEnum 0 == Just A
assert $ toEnum 1 == Just B
assert $ toEnum 2 == Just C
assert $ toEnum 3 == Just D
assert $ toEnum 4 == Just E