@@ -2,7 +2,9 @@ module Data.Int.AtLeast
2
2
( IntAL -- Constructor not exported. Use fromInt, fromInt' or clamp
3
3
, fromInt
4
4
, fromInt'
5
+ , fromMin
5
6
, clamp
7
+ , fromLength
6
8
, toInt
7
9
, toNumber
8
10
, extent
@@ -28,7 +30,7 @@ module Data.Int.AtLeast
28
30
29
31
import Prelude hiding (lcm , gcd )
30
32
31
- import Data.Array (length ) as Array
33
+ import Data.Array (length , range ) as Array
32
34
import Data.Array.NonEmpty (NonEmptyArray , cons' , length , singleton ) as NEA
33
35
import Data.Enum (class Enum )
34
36
import Data.EuclideanRing (gcd ) as Int
@@ -95,13 +97,31 @@ fromInt' i =
95
97
else crashWith $
96
98
" Cannot convert Int " <> show i <> " to IntAL " <> show minInt
97
99
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
98
106
clamp :: ∀ (min :: Int ). Reflectable min Int => Int -> IntAL min
99
107
clamp i =
100
108
let
101
109
minInt = reflectType (Proxy :: _ min )
102
110
in
103
111
if i >= minInt then IntAL i else IntAL minInt
104
112
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
+
105
125
-- | Convert an `IntAL` to an `Int`
106
126
toInt :: ∀ (min :: Int ). IntAL min -> Int
107
127
toInt (IntAL i) = i
0 commit comments