-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix windowed and add docs for windowed and pairwise
- Loading branch information
Showing
6 changed files
with
128 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
}; | ||
|