@@ -2,7 +2,9 @@ module Data.Int.AtLeast
22 ( IntAL -- Constructor not exported. Use fromInt, fromInt' or clamp
33 , fromInt
44 , fromInt'
5+ , fromMin
56 , clamp
7+ , fromLength
68 , toInt
79 , toNumber
810 , extent
@@ -28,7 +30,7 @@ module Data.Int.AtLeast
2830
2931import Prelude hiding (lcm , gcd )
3032
31- import Data.Array (length ) as Array
33+ import Data.Array (length , range ) as Array
3234import Data.Array.NonEmpty (NonEmptyArray , cons' , length , singleton ) as NEA
3335import Data.Enum (class Enum )
3436import Data.EuclideanRing (gcd ) as Int
@@ -95,13 +97,31 @@ fromInt' i =
9597 else crashWith $
9698 " Cannot convert Int " <> show i <> " to IntAL " <> show minInt
9799
100+ -- | Construct an `IntAL` equal to its type-level minimum
101+ fromMin :: ∀ (min :: Int ). Reflectable min Int => IntAL min
102+ fromMin = IntAL $ reflectType (Proxy :: _ min )
103+
104+ -- | Convert an `Int` to an `IntAL`, increasing the value to the type-level
105+ -- | minimum if required
98106clamp :: ∀ (min :: Int ). Reflectable min Int => Int -> IntAL min
99107clamp i =
100108 let
101109 minInt = reflectType (Proxy :: _ min )
102110 in
103111 if i >= minInt then IntAL i else IntAL minInt
104112
113+ -- | Construct an `Array` of `IntAL`. To construct an `ArrayAL` of `IntAL` see
114+ -- | Data.Array.AtLeast.fromLength'
115+ fromLength
116+ :: ∀ (min_val :: Int ) (min_len :: Int )
117+ . Compare (-1) min_len LT
118+ => IntAL min_val
119+ -> IntAL min_len
120+ -> Array (IntAL min_val )
121+ fromLength (IntAL first) (IntAL len) =
122+ if len == 0 then []
123+ else IntAL <$> Array .range first (first + len - 1 )
124+
105125-- | Convert an `IntAL` to an `Int`
106126toInt :: ∀ (min :: Int ). IntAL min -> Int
107127toInt (IntAL i) = i
0 commit comments