Skip to content

Commit

Permalink
fix windowed and add docs for windowed and pairwise
Browse files Browse the repository at this point in the history
  • Loading branch information
mo-ba committed Oct 21, 2023
1 parent a28bae0 commit 37c81bc
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 12 deletions.
8 changes: 8 additions & 0 deletions docs/src/config-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ export default {
"text": "minBy",
"link": "en/array/operator/minBy"
},
{
"text": "pairwise",
"link": "en/array/operator/pairwise"
},
{
"text": "partition",
"link": "en/array/operator/partition"
Expand Down Expand Up @@ -182,6 +186,10 @@ export default {
"text": "uniqueBy",
"link": "en/array/operator/uniqueBy"
},
{
"text": "windowed",
"link": "en/array/operator/windowed"
},
{
"text": "zip",
"link": "en/array/operator/zip"
Expand Down
43 changes: 43 additions & 0 deletions docs/src/pages/en/array/operator/pairwise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
title: pairwise
description: pairwise
layout: ../../../../layouts/MainLayout.astro
---
Creates pairs of adjacent elements in an array.

## Parameters

- `array` (Array): An array of elements for which pairs of adjacent elements are to be created.

## Returns

- (Array of Arrays): An array of pairs, where each pair is represented as an array containing two adjacent elements from the input array.


## Example

```ts
const inputArray = [1, 2, 3, 4, 5, 6];
const windowSize = 3;

const resultWindows = pairwise (windowSize)(inputArray);
// -> [[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6]]
```

## Notes

- The function creates pairs of adjacent elements in the input array.
If the input array has N elements, the resulting array will have N-1 pairs.
- The input array remains unaltered, and a new array of pairs is returned.
- If the input array has less than two elements, an empty array is returned
since there are no adjacent elements to pair.
- The function `pairwise` is an optimized version of `windowed(2)`.
- This function does not modify the original array.



## See Also

- [windowed](./windowed)
- [allPairs](./allPairs)
- [zip](./zip)
57 changes: 57 additions & 0 deletions docs/src/pages/en/array/operator/windowed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
title: windowed
description: windowed
layout: ../../../../layouts/MainLayout.astro
---
Splits an array into subarrays (windows) of a specified size,
moving the window by one element at a time.
## Type

```ts
type windowed = (windowSize: number) => <T>(array: T[]) => T[][]
```
## Parameters
- `windowSize` (number): The size of each window.
- `array` (Array): An array of elements to split into windows.
## Returns
- (Array of Arrays): An array of subarrays (windows) containing
elements from the input array.
## Example
```ts
const inputArray = [1, 2, 3, 4, 5, 6];
const windowSize = 3;

const resultWindows = windowed(windowSize)(inputArray);
// -> [[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6]]
```

## Notes

- The `windowSize` parameter determines the size of each window.
It should be a positive integer (greater than 1).
- If the `windowSize` is less than 1, an error will be thrown.

- The function splits the input array into subarrays (windows)
of the specified size, moving the window one element at a time.
If the input array is too short to create a window of the specified
size, an empty array is included in the result.

- The input array remains unaltered, and a new array of subarrays
is returned.

- If the `windowSize` is greater than the length of the input array,
the result will be an empty array.

- If the input array is empty, an empty array is returned.



## See Also

- [pairwise](./pairwise)
2 changes: 1 addition & 1 deletion spec/core/array/operator/pairwise.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {checkThrow} from '../../../support/checkThrow';
describe('pairwise', () => {
it('should pairwise', () => {
expect(pairwise([1, 2, 3, 4])).to.eql([[1, 2], [2, 3], [3, 4]]);
expect(pairwise([1, 2, 3, 4])).to.eql(windowed<number>(2)([1, 2, 3, 4]));
expect(pairwise([1, 2, 3, 4])).to.eql(windowed(2)([1, 2, 3, 4]));
expect(pairwise([1])).to.eql([]);
expect(pairwise([])).to.eql([]);
});
Expand Down
5 changes: 5 additions & 0 deletions spec/core/array/operator/windowed.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ describe('windowed', () => {
expect(fn([1])).to.eql([]);
});

it('should throw if windowSize is less than 1', () => {
expect(() => windowed(0)).to.throw(`windowSize must not be less than 1. (0 given)`);
expect(() => windowed(-1)).to.throw(`windowSize must not be less than 1. (-1 given)`);
});


it('should return empty', () => {
const fn = windowed(2);
Expand Down
25 changes: 14 additions & 11 deletions src/array/operator/windowed.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
export const windowed = <T>(windowSize: number) => (array: T[]): T[][] => {
const result = [];

for (let i = 0; i <= array.length - windowSize; i++) {
const window: T[] = [];
result.push(window);
for (let j = i; j < i + windowSize; j++) {
window.push(array[j]);
}
export const windowed = (windowSize: number) => {
if (windowSize < 1) {
throw Error(`windowSize must not be less than 1. (${windowSize} given)`);
}
return result;

return <T>(array: T[]): T[][] => {
const result = [];
for (let i = 0; i <= array.length - windowSize; i++) {
const window: T[] = [];
result.push(window);
for (let j = i; j < i + windowSize; j++) {
window.push(array[j]);
}
}
return result;
};
};

0 comments on commit 37c81bc

Please sign in to comment.