@@ -187,6 +187,7 @@ singleton :: forall a. a -> Array a
187187singleton a = [a]
188188
189189-- | Create an array containing a range of integers, including both endpoints.
190+ -- | If you want to control the step-size, see `rangeWithStep`
190191-- | ```purescript
191192-- | range 2 5 = [2, 3, 4, 5]
192193-- | ```
@@ -1294,8 +1295,9 @@ unsafeIndex = unsafeIndexImpl
12941295
12951296foreign import unsafeIndexImpl :: forall a . Array a -> Int -> a
12961297
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`
12991301-- |
13001302-- | ```purescript
13011303-- | sliding [1,2,3,4,5] = [(Tuple 1 2),(Tuple 2 3),(Tuple 3 4),(Tuple 4 5)]
@@ -1307,10 +1309,13 @@ sliding l = zip l (drop 1 l)
13071309-- | Takes an arrays and returns an array of arrays as a "sliding window" view of this array.
13081310-- | Illegal arguments result in an empty Array.
13091311-- |
1312+ -- | As you can see in the example below, the last window might not get filled completely.
1313+ -- | Its size `s` will be `1 <= s <= size`.
1314+ -- |
13101315-- | ```purescript
13111316-- | > 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]]
1317+ -- | > 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]]
1318+ -- | > 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]]
13141319-- | ```
13151320-- |
13161321slidingSizeStep :: forall a . Int -> Int -> Array a -> Array (NonEmptyArray a )
@@ -1334,37 +1339,37 @@ slidingSizeStep size step array
13341339rangeWithStep :: Int -> Int -> Int -> Array Int
13351340rangeWithStep start end step = rangeWithStep' start end step identity
13361341
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.
1342+ -- | Works just like rangeWithStep, but also uses a function to map each integer.
1343+ -- | `rangeWithStep' start end step f` is the same as `map f $ rangeWithStep start end step`,
1344+ -- | but without the extra intermediate array allocation.
13381345-- |
13391346-- | ```purescript
13401347-- | > rangeWithStep' 0 6 2 identity = [0,2,4,6]
13411348-- | > rangeWithStep' 0 6 2 (add 3) = [3,5,7,9]
1349+ -- | > rangeWithStep' 0 (-6) (-2) (add 3) = [3,1,-1,-3]
13421350-- | ```
13431351rangeWithStep' :: forall t . Int -> Int -> Int -> (Int -> t ) -> Array t
13441352rangeWithStep' 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- )
1353+ if not isValid
1354+ then []
1355+ else STA .run do
1356+ let
1357+ helper current acc =
1358+ if hasReachedEnd current then
1359+ pure acc
1360+ else do
1361+ void $ STA .push (fn current) acc
1362+ helper (current + step) acc
1363+ arr <- STA .new
1364+ _ <- helper start arr
1365+ pure arr
1366+ where
1367+ isValid =
1368+ step /= 0 &&
1369+ if end >= start
1370+ then step > 0
1371+ else step < 0
1372+ hasReachedEnd current =
1373+ if step > 0
1374+ then current > end
1375+ else current < end
0 commit comments