Skip to content

Commit c71cbaf

Browse files
committed
improved docs and tests based on code review
1 parent da87cc6 commit c71cbaf

File tree

2 files changed

+38
-31
lines changed

2 files changed

+38
-31
lines changed

src/Data/Array.purs

+35-31
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ singleton :: forall a. a -> Array a
187187
singleton a = [a]
188188

189189
-- | Create an array containing a range of integers, including both endpoints.
190+
-- | If you want to control the step-size of the range, see `rangeWithStep`
190191
-- | ```purescript
191192
-- | range 2 5 = [2, 3, 4, 5]
192193
-- | ```
@@ -1294,8 +1295,9 @@ unsafeIndex = unsafeIndexImpl
12941295

12951296
foreign 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)]
@@ -1306,11 +1308,12 @@ sliding l = zip l (drop 1 l)
13061308

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.
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`.
13091312
-- |
13101313
-- | ```purescript
13111314
-- | > 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]]
13141317
-- | ```
13151318
-- |
13161319
slidingSizeStep :: forall a. Int -> Int -> Array a -> Array (NonEmptyArray a)
@@ -1334,37 +1337,38 @@ slidingSizeStep size step array
13341337
rangeWithStep :: Int -> Int -> Int -> Array Int
13351338
rangeWithStep start end step = rangeWithStep' start end step identity
13361339

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
13381342
-- |
13391343
-- | ```purescript
13401344
-- | > rangeWithStep' 0 6 2 identity = [0,2,4,6]
13411345
-- | > rangeWithStep' 0 6 2 (add 3) = [3,5,7,9]
1346+
-- | > rangeWithStep' 0 (-6) (-2) (add 3) = [3,1,-1,-3]
13421347
-- | ```
13431348
rangeWithStep' :: forall t. Int -> Int -> Int -> (Int -> t) -> Array t
13441349
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

test/Test/Data/Array.purs

+3
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,10 @@ testArray = do
469469

470470
log "sliding"
471471
assert $ A.sliding [ 1, 2, 3, 4, 5 ] == [ (Tuple 1 2), (Tuple 2 3), (Tuple 3 4), (Tuple 4 5) ]
472+
assert $ A.sliding nil == []
472473

473474
log "slidingSizeStep"
475+
assert $ A.slidingSizeStep 2 1 (A.range 0 4) == ([[0,1],[1,2],[2,3],[3,4],[4]] <#> nea)
474476
assert $ A.slidingSizeStep 3 2 (A.range 0 10) == ([[0,1,2],[2,3,4],[4,5,6],[6,7,8],[8,9,10],[10]] <#> nea)
475477
assert $ A.slidingSizeStep 3 3 (A.range 0 10) == ([[0,1,2],[3,4,5],[6,7,8],[9,10]] <#> nea)
476478
assert $ A.slidingSizeStep 3 (-2) (A.range 0 10) == []
@@ -485,6 +487,7 @@ testArray = do
485487
log "rangeWithStep'"
486488
assert $ A.rangeWithStep 0 6 2 == A.rangeWithStep' 0 6 2 identity
487489
assert $ A.rangeWithStep' 0 6 2 (add 3) == [3, 5, 7, 9]
490+
assert $ A.rangeWithStep' 0 (-6) (-2) (add 3) == [ 3, 1, -1, -3 ]
488491

489492
nea :: Array ~> NEA.NonEmptyArray
490493
nea = unsafePartial fromJust <<< NEA.fromArray

0 commit comments

Comments
 (0)