Skip to content

Commit 82a50c0

Browse files
committed
added creator-function rangeWithStep and window-functions sliding and slidingWithSizeAndStep
1 parent c0aa317 commit 82a50c0

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

src/Data/Array.purs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module Data.Array
3232
, toUnfoldable
3333
, singleton
3434
, (..), range
35+
, rangeWithStep
3536
, replicate
3637
, some
3738
, many
@@ -87,6 +88,8 @@ module Data.Array
8788
, scanl
8889
, scanr
8990

91+
, sliding
92+
, slidingWithSizeAndStep
9093
, sort
9194
, sortBy
9295
, sortWith
@@ -1289,3 +1292,72 @@ unsafeIndex :: forall a. Partial => Array a -> Int -> a
12891292
unsafeIndex = unsafeIndexImpl
12901293

12911294
foreign import unsafeIndexImpl :: forall a. Array a -> Int -> a
1295+
1296+
-- | Takes an arrays of length n and returns an array of Tuples with length n-1.
1297+
-- | It's a "sliding window" view of this array with a window size of 2 and a step size of 1.
1298+
-- |
1299+
-- | ```purescript
1300+
-- | sliding [1,2,3,4,5] = [(Tuple 1 2),(Tuple 2 3),(Tuple 3 4),(Tuple 4 5)]
1301+
-- | ```
1302+
-- |
1303+
sliding :: forall a. Array a -> Array (Tuple a a)
1304+
sliding l = zip l (drop 1 l)
1305+
1306+
-- | Takes an arrays and returns an array of arrays as a "sliding window" view of this array.
1307+
-- | Illegal arguments result in an empty Array.
1308+
-- |
1309+
-- | ```purescript
1310+
-- | > import Data.Array (range)
1311+
-- | > slidingWithSizeAndStep 3 2 (range 0 10) = [[0,1,2],[2,3,4],[4,5,6],[6,7,8],[8,9,10],[10]]
1312+
-- | > slidingWithSizeAndStep 3 3 (range 0 10) = [[0,1,2],[3,4,5],[6,7,8],[9,10]]
1313+
-- | ```
1314+
-- |
1315+
slidingWithSizeAndStep :: forall a. Int -> Int -> Array a -> Array (Array a)
1316+
slidingWithSizeAndStep size step array =
1317+
let
1318+
maxIndex = (length array) - 1
1319+
1320+
indices = rangeWithStep 0 maxIndex step
1321+
1322+
isValid = size > 0 && step > 0
1323+
in
1324+
if isValid then
1325+
indices <#> \i -> slice i (i + size) array
1326+
else
1327+
[]
1328+
1329+
-- | Create an array containing a range of integers with a given step size, including both endpoints.
1330+
-- | Illegal arguments result in an empty Array.
1331+
-- |
1332+
-- | ```purescript
1333+
-- | > rangeWithStep 0 6 2 = [0,2,4,6]
1334+
-- | > rangeWithStep 0 (-6) (-2) = [0,-2,-4,-6]
1335+
-- | ```
1336+
-- |
1337+
rangeWithStep :: Int -> Int -> Int -> Array Int
1338+
rangeWithStep start endIndex step =
1339+
let
1340+
isValid =
1341+
step /= 0
1342+
&& if endIndex >= start then
1343+
step > 0
1344+
else
1345+
step < 0
1346+
1347+
hasReachedEnd curr =
1348+
if step > 0 then
1349+
curr > endIndex
1350+
else
1351+
curr < endIndex
1352+
1353+
helper :: Int -> Array Int -> Array Int
1354+
helper currentIndex acc =
1355+
if hasReachedEnd currentIndex then
1356+
acc
1357+
else
1358+
helper (currentIndex + step) (snoc acc currentIndex)
1359+
in
1360+
if isValid then
1361+
helper start []
1362+
else
1363+
[]

test/Test/Data/Array.purs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,21 @@ testArray = do
467467
, [4,0,0,1,25,36,458,5842,23757]
468468
]
469469

470+
log "sliding"
471+
assert $ A.sliding [ 1, 2, 3, 4, 5 ] == [ (Tuple 1 2), (Tuple 2 3), (Tuple 3 4), (Tuple 4 5) ]
472+
473+
log "slidingWithStepAndSize"
474+
assert $ A.slidingWithSizeAndStep 3 2 (A.range 0 10) == [[0,1,2],[2,3,4],[4,5,6],[6,7,8],[8,9,10],[10]]
475+
assert $ A.slidingWithSizeAndStep 3 3 (A.range 0 10) == [[0,1,2],[3,4,5],[6,7,8],[9,10]]
476+
assert $ A.slidingWithSizeAndStep 3 (-2) (A.range 0 10) == []
477+
assert $ A.slidingWithSizeAndStep (-3) (2) (A.range 0 10) == []
478+
479+
log "rangeWithStep"
480+
assert $ A.rangeWithStep 0 6 2 == [ 0, 2, 4, 6 ]
481+
assert $ A.rangeWithStep 0 (-6) (-2) == [ 0, -2, -4, -6 ]
482+
assert $ A.rangeWithStep 0 6 (-2) == []
483+
assert $ A.rangeWithStep 0 (-6) (2) == []
484+
470485
nea :: Array ~> NEA.NonEmptyArray
471486
nea = unsafePartial fromJust <<< NEA.fromArray
472487

0 commit comments

Comments
 (0)