@@ -187,6 +187,7 @@ singleton :: forall a. a -> Array a
187
187
singleton a = [a]
188
188
189
189
-- | Create an array containing a range of integers, including both endpoints.
190
+ -- | If you want to control the step-size, see `rangeWithStep`
190
191
-- | ```purescript
191
192
-- | range 2 5 = [2, 3, 4, 5]
192
193
-- | ```
@@ -1294,8 +1295,9 @@ unsafeIndex = unsafeIndexImpl
1294
1295
1295
1296
foreign import unsafeIndexImpl :: forall a . Array a -> Int -> a
1296
1297
1297
- -- | Takes an arrays of length n and returns an array of Tuples with length n-1.
1298
- -- | It's a "sliding window" view of this array with a window size of 2 and a step size of 1.
1298
+ -- | Returns an array where each value represents a "sliding window" of the given array with a window of size 2 and a step size of 1.
1299
+ -- |
1300
+ -- | If you need more control over the size of the window and how far to step before making a new window, see `slidingSizeStep`
1299
1301
-- |
1300
1302
-- | ```purescript
1301
1303
-- | sliding [1,2,3,4,5] = [(Tuple 1 2),(Tuple 2 3),(Tuple 3 4),(Tuple 4 5)]
@@ -1306,11 +1308,12 @@ sliding l = zip l (drop 1 l)
1306
1308
1307
1309
-- | Takes an arrays and returns an array of arrays as a "sliding window" view of this array.
1308
1310
-- | Illegal arguments result in an empty Array.
1311
+ -- | As you can see in the example below, the last window might not get filled completely. Its size `s` will be `1 <= s <= size`.
1309
1312
-- |
1310
1313
-- | ```purescript
1311
1314
-- | > import Data.Array (range)
1312
- -- | > slidingSizeStep 3 2 (range 0 10) = [[0,1,2],[2,3,4],[4,5,6],[6,7,8],[8,9,10],[10]]
1313
- -- | > slidingSizeStep 3 3 (range 0 10) = [[0,1,2],[3,4,5],[6,7,8],[9,10]]
1315
+ -- | > slidingSizeStep 3 2 [0,1,2,3,4,5,6,7,8,9,10] = [[0,1,2],[2,3,4],[4,5,6],[6,7,8],[8,9,10],[10]]
1316
+ -- | > slidingSizeStep 3 3 [0,1,2,3,4,5,6,7,8,9,10] = [[0,1,2],[3,4,5],[6,7,8],[9,10]]
1314
1317
-- | ```
1315
1318
-- |
1316
1319
slidingSizeStep :: forall a . Int -> Int -> Array a -> Array (NonEmptyArray a )
@@ -1334,37 +1337,38 @@ slidingSizeStep size step array
1334
1337
rangeWithStep :: Int -> Int -> Int -> Array Int
1335
1338
rangeWithStep start end step = rangeWithStep' start end step identity
1336
1339
1337
- -- | Helper function to produce an array of elements like `rangeWithStep` with start, end and step, but immediatelly mapping over the result to work on one single mutable array.
1340
+ -- | Works just like rangeWithStep, but also uses a function to map each integer.
1341
+ -- | `rangeWithStep' start end step f` is the same as `map f $ rangeWithStep start end step`, but without the extra intermediate array allocation
1338
1342
-- |
1339
1343
-- | ```purescript
1340
1344
-- | > rangeWithStep' 0 6 2 identity = [0,2,4,6]
1341
1345
-- | > rangeWithStep' 0 6 2 (add 3) = [3,5,7,9]
1346
+ -- | > rangeWithStep' 0 (-6) (-2) (add 3) = [3,1,-1,-3]
1342
1347
-- | ```
1343
1348
rangeWithStep' :: forall t . Int -> Int -> Int -> (Int -> t ) -> Array t
1344
1349
rangeWithStep' start end step fn =
1345
- STA .run
1346
- ( do
1347
- let
1348
- isValid =
1349
- step /= 0
1350
- && if end >= start then
1351
- step > 0
1352
- else
1353
- step < 0
1354
-
1355
- hasReachedEnd current =
1356
- if step > 0 then
1357
- current > end
1358
- else
1359
- current < end
1360
-
1361
- helper current acc =
1362
- if hasReachedEnd current then
1363
- pure acc
1364
- else do
1365
- void $ STA .push (fn current) acc
1366
- helper (current + step) acc
1367
- arr <- STA .new
1368
- void $ helper start arr
1369
- pure arr
1370
- )
1350
+ if not isValid
1351
+ then []
1352
+ else STA .run
1353
+ ( do
1354
+ let
1355
+ helper current acc =
1356
+ if hasReachedEnd current then
1357
+ pure acc
1358
+ else do
1359
+ void $ STA .push (fn current) acc
1360
+ helper (current + step) acc
1361
+ arr <- STA .new
1362
+ void $ helper start arr
1363
+ pure arr
1364
+ )
1365
+ where
1366
+ isValid =
1367
+ step /= 0 &&
1368
+ if end >= start
1369
+ then step > 0
1370
+ else step < 0
1371
+ hasReachedEnd current =
1372
+ if step > 0
1373
+ then current > end
1374
+ else current < end
0 commit comments