-
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.
- Loading branch information
Showing
8 changed files
with
256 additions
and
147 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
--- | ||
title: kernel | ||
description: kernel | ||
layout: ../../../../layouts/MainLayout.astro | ||
--- | ||
Splits an array into subarrays (kernels) of a specified `windowSize`, | ||
moving the window by `stride` elements at a time. | ||
## Type | ||
|
||
```ts | ||
type kernel = (stride: number) => (windowSize: number) => <T>(array: T[]) => T[][] | ||
``` | ||
## Parameters | ||
- `stride` (number): The step size to move the window in the input array. Must be greater than or equal to 1. | ||
- `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, 7, 8, 9, 10]; | ||
const stride = 2; | ||
const windowSize = 3; | ||
|
||
const resultWindows = kernel(stride)(windowSize)(inputArray); | ||
// -> [[1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9]] | ||
``` | ||
|
||
## Notes | ||
- The `stride` parameter determines the step size to move the window | ||
in the input `array`. | ||
- It should be a positive integer greater than or equal to 1. | ||
- If the `stride` is less than 1, an error will be thrown. | ||
|
||
- The `windowSize` parameter determines the size of each window. | ||
It should be a positive integer (greater than or equal to 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 `stride` elements 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 | ||
|
||
- [windowed](./windowed) | ||
- [stride](./stride) | ||
- [pairwise](./pairwise) | ||
- [chunkBySize](./chunkBySize) |
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
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,63 @@ | ||
import {expect} from 'chai'; | ||
|
||
import {checkThrow} from '../../../support/checkThrow'; | ||
import {chunkBySize, kernel, range, windowed} from '../../../../src/array'; | ||
|
||
describe('kernel', () => { | ||
|
||
it('should kernel 1:2', () => { | ||
const array = [1, 2, 3, 4]; | ||
const fn = kernel(2)(2); | ||
expect(fn(array)).to.eql([[1, 2], [3, 4]]); | ||
expect(fn([1, 2])).to.eql([[1, 2]]); | ||
expect(fn([1])).to.eql([]); | ||
}); | ||
it('should kernel 2:3', () => { | ||
const array = [1, 2, 3, 4, 5, 6, 7]; | ||
const fn = kernel(2)(3); | ||
expect(fn(array)).to.eql([[1, 2, 3], [3, 4, 5], [5, 6, 7]]); | ||
expect(fn([1, 2, 3])).to.eql([[1, 2, 3]]); | ||
expect(fn([1, 2])).to.eql([]); | ||
expect(fn([1])).to.eql([]); | ||
}); | ||
describe('iterate', () => { | ||
for (let i = 1; i < 50; i += Math.ceil(Math.random() * 5)) { | ||
|
||
it(`should kernel 1:${i} equals windowed ${i}`, () => { | ||
const array = range(0, 100); | ||
const fn = kernel(1)(i); | ||
expect(fn(array)).to.eql(windowed(i)(array)); | ||
}); | ||
it(`should kernel ${i}:${i} equals chunkBySize ${i} if array length is multiple of ${i}`, () => { | ||
const array = range(0, Math.floor(100 / i) * i); | ||
const fn = kernel(i)(i); | ||
expect(fn(array)).to.eql(chunkBySize(i)(array)); | ||
}); | ||
} | ||
}); | ||
|
||
it('should return empty if window greater than length', () => { | ||
const fn = kernel(1)(2); | ||
expect(fn([1])).to.eql([]); | ||
}); | ||
|
||
it('should throw if windowSize is less than 1', () => { | ||
expect(() => kernel(1)(0)).to.throw(`windowSize must not be less than 1. (0 given)`); | ||
expect(() => kernel(1)(-1)).to.throw(`windowSize must not be less than 1. (-1 given)`); | ||
}); | ||
it('should throw if stride is less than 1', () => { | ||
expect(() => kernel(0)(1)).to.throw(`stride must not be less than 1. (0 given)`); | ||
expect(() => kernel(-1)(1)).to.throw(`stride must not be less than 1. (-1 given)`); | ||
}); | ||
|
||
|
||
it('should return empty', () => { | ||
const fn = kernel(1)(2); | ||
expect(fn([])).to.eql([]); | ||
}); | ||
|
||
it('should throw if null or undefined', () => { | ||
const fn = kernel(1)(2); | ||
checkThrow(fn); | ||
}); | ||
}); |
Oops, something went wrong.