Skip to content

Commit 88103e3

Browse files
Introduce fromLength functions
1 parent 8109065 commit 88103e3

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/Data/Array/AtLeast.purs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module Data.Array.ArrayAL
2222
, appendArray
2323
, prependArray
2424
, range
25+
, fromLength
2526
, concat
2627
, intersperse
2728

@@ -70,6 +71,7 @@ import Data.Foldable (class Foldable)
7071
import Data.FoldableWithIndex (class FoldableWithIndex)
7172
import Data.FunctorWithIndex (class FunctorWithIndex)
7273
import Data.Int.AtLeast (IntAL, fromInt', toInt)
74+
import Data.Int.AtLeast (fromLength) as IntAL
7375
import Data.Maybe (Maybe(Nothing, Just), fromJust)
7476
import Data.Reflectable (class Reflectable, reflectType)
7577
import Data.Semigroup.Foldable (class Foldable1, foldMap1DefaultL)
@@ -263,6 +265,26 @@ range i = ArrayAL $ toIntAL <$> values
263265
values :: Array Int
264266
values = Array.range (reflectType (Proxy :: _ min)) (toInt i)
265267

268+
-- | Construct an `ArrayAL` of sequential `IntAL` from a starting `IntAL` and
269+
-- | the required length of the `ArrayAL`
270+
fromLength
271+
:: (min_len :: Int) (min_val :: Int)
272+
. Compare (-1) min_len LT
273+
=> IntAL min_val
274+
-> IntAL min_len
275+
-> ArrayAL min_len (IntAL min_val)
276+
fromLength first len = ArrayAL $ IntAL.fromLength first len
277+
278+
-- | Construct an `ArrayAL` of sequential integers from a starting integer and
279+
-- | the required length of the `ArrayAL`
280+
fromLength'
281+
:: (min :: Int). Compare (-1) min LT => Int -> IntAL min -> ArrayAL min Int
282+
fromLength' first len =
283+
let
284+
lenInt = toInt len
285+
in
286+
ArrayAL if lenInt == 0 then [] else Array.range first (first + lenInt - 1)
287+
266288
concat
267289
:: (m :: Int) (n :: Int) (mn :: Int) (a :: Type)
268290
. Compare (-1) m LT

src/Data/Int/AtLeast.purs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

2931
import Prelude hiding (lcm, gcd)
3032

31-
import Data.Array (length) as Array
33+
import Data.Array (length, range) as Array
3234
import Data.Array.NonEmpty (NonEmptyArray, cons', length, singleton) as NEA
3335
import Data.Enum (class Enum)
3436
import 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
98106
clamp :: (min :: Int). Reflectable min Int => Int -> IntAL min
99107
clamp 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`
106126
toInt :: (min :: Int). IntAL min -> Int
107127
toInt (IntAL i) = i

0 commit comments

Comments
 (0)