Skip to content

Commit fdf0b15

Browse files
f15ueffect-bot
authored andcommitted
feat: add Array.window (#4477)
1 parent 7ea308b commit fdf0b15

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": minor
3+
---
4+
5+
Add `Array.window` function

packages/effect/dtslint/Array.tst.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,18 @@ describe("Array", () => {
942942
.type.toBe<[[string, ...Array<string>], ...Array<[string, ...Array<string>]>]>()
943943
})
944944

945+
it("window", () => {
946+
// Array
947+
expect(Array.window(strings, 2)).type.toBe<Array<Array<string>>>()
948+
expect(pipe(strings, Array.window(2))).type.toBe<Array<Array<string>>>()
949+
expect(Array.window(2)(strings)).type.toBe<Array<Array<string>>>()
950+
951+
// NonEmptyArray
952+
expect(Array.window(nonEmptyStrings, 2)).type.toBe<Array<Array<string>>>()
953+
expect(pipe(nonEmptyStrings, Array.window(2))).type.toBe<Array<Array<string>>>()
954+
expect(Array.window(2)(nonEmptyStrings)).type.toBe<Array<Array<string>>>()
955+
})
956+
945957
it("reverse", () => {
946958
// Array
947959
expect(Array.reverse(strings)).type.toBe<Array<string>>()

packages/effect/src/Array.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,6 +1969,37 @@ export const chunksOf: {
19691969
return []
19701970
})
19711971

1972+
/**
1973+
* Creates sliding windows of size `n` from an `Iterable`.
1974+
* If the number of elements is less than `n` or if `n` is not greater than zero,
1975+
* an empty array is returned.
1976+
*
1977+
* @example
1978+
* ```ts
1979+
* import { Array } from "effect"
1980+
*
1981+
* const numbers = [1, 2, 3, 4, 5]
1982+
* assert.deepStrictEqual(Array.window(numbers, 3), [[1, 2, 3], [2, 3, 4], [3, 4, 5]])
1983+
* assert.deepStrictEqual(Array.window(numbers, 6), [])
1984+
* ```
1985+
*
1986+
* @category splitting
1987+
* @since 3.13.2
1988+
*/
1989+
export const window: {
1990+
(n: number): <A>(self: Iterable<A>) => Array<Array<A>>
1991+
<A>(self: Iterable<A>, n: number): Array<Array<A>>
1992+
} = dual(2, <A>(self: Iterable<A>, n: number): Array<Array<A>> => {
1993+
const input = fromIterable(self)
1994+
if (n > 0 && isNonEmptyReadonlyArray(input)) {
1995+
return Array.from(
1996+
{ length: input.length - (n - 1) },
1997+
(_, index) => input.slice(index, index + n)
1998+
)
1999+
}
2000+
return []
2001+
})
2002+
19722003
/**
19732004
* Group equal, consecutive elements of a `NonEmptyReadonlyArray` into `NonEmptyArray`s using the provided `isEquivalent` function.
19742005
*

packages/effect/test/Array.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,18 @@ describe("Array", () => {
956956
assertSingleChunk(Arr.make(1, 2), 3)
957957
})
958958

959+
it("window", () => {
960+
deepStrictEqual(Arr.window(2)([]), [])
961+
962+
deepStrictEqual(Arr.window(2)([1, 2, 3, 4, 5]), [[1, 2], [2, 3], [3, 4], [4, 5]])
963+
deepStrictEqual(Arr.window(3)([1, 2, 3, 4, 5]), [[1, 2, 3], [2, 3, 4], [3, 4, 5]])
964+
965+
// n out of bounds
966+
deepStrictEqual(Arr.window([1, 2, 3, 4, 5], 6), [])
967+
deepStrictEqual(Arr.window([1, 2, 3, 4, 5], 0), [])
968+
deepStrictEqual(Arr.window([1, 2, 3, 4, 5], -1), [])
969+
})
970+
959971
it("min", () => {
960972
deepStrictEqual(Arr.min(Num.Order)([2, 1, 3]), 1)
961973
deepStrictEqual(Arr.min(Num.Order)([3]), 3)

0 commit comments

Comments
 (0)