Skip to content

Commit 25dd1cd

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

File tree

2 files changed

+41
-31
lines changed

2 files changed

+41
-31
lines changed

src/Data/Array.purs

+38-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, 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)]
@@ -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
-- |
13161321
slidingSizeStep :: forall a. Int -> Int -> Array a -> Array (NonEmptyArray a)
@@ -1334,37 +1339,39 @@ slidingSizeStep size step array
13341339
rangeWithStep :: Int -> Int -> Int -> Array Int
13351340
rangeWithStep 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
-- | ```
13431351
rangeWithStep' :: forall t. Int -> Int -> Int -> (Int -> t) -> Array t
13441352
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-
)
1353+
if not isValid
1354+
then []
1355+
else STA.run
1356+
( do
1357+
let
1358+
helper current acc =
1359+
if hasReachedEnd current then
1360+
pure acc
1361+
else do
1362+
void $ STA.push (fn current) acc
1363+
helper (current + step) acc
1364+
arr <- STA.new
1365+
void $ helper start arr
1366+
pure arr
1367+
)
1368+
where
1369+
isValid =
1370+
step /= 0 &&
1371+
if end >= start
1372+
then step > 0
1373+
else step < 0
1374+
hasReachedEnd current =
1375+
if step > 0
1376+
then current > end
1377+
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)