Skip to content

Commit

Permalink
rename kernel to strideWindowed
Browse files Browse the repository at this point in the history
  • Loading branch information
mo-ba committed Oct 21, 2023
1 parent ab71d0f commit 702df77
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 34 deletions.
8 changes: 4 additions & 4 deletions docs/src/config-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ export default {
"text": "isEmpty",
"link": "en/array/operator/isEmpty"
},
{
"text": "kernel",
"link": "en/array/operator/kernel"
},
{
"text": "last",
"link": "en/array/operator/last"
Expand Down Expand Up @@ -154,6 +150,10 @@ export default {
"text": "stride",
"link": "en/array/operator/stride"
},
{
"text": "strideWindowed",
"link": "en/array/operator/strideWindowed"
},
{
"text": "tail",
"link": "en/array/operator/tail"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
---
title: kernel
description: kernel
title: strideWindowed
description: strideWindowed
layout: ../../../../layouts/MainLayout.astro
---
Splits an array into subarrays (kernels) of a specified `windowSize`,
Splits an array into subarrays (windows) 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[][]
type strideWindowed = (stride: number) => (windowSize: number) => <T>(array: T[]) => T[][]
```
## Parameters
Expand All @@ -29,7 +29,7 @@ const inputArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const stride = 2;
const windowSize = 3;

const resultWindows = kernel(stride)(windowSize)(inputArray);
const resultWindows = strideWindowed(stride)(windowSize)(inputArray);
// -> [[1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9]]
```

Expand Down
2 changes: 1 addition & 1 deletion docs/src/pages/en/array/operator/windowed.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ the result will be an empty array.

## See Also

- [kernel](./kernel)
- [strideWindowed](./strideWindowed)
- [stride](./stride)
- [pairwise](./pairwise)
6 changes: 3 additions & 3 deletions spec/core/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('array', () => {
'findIndexBack', 'flatten', 'fold',
'foldBack', 'forall', 'groupBy', 'head',
'init', 'insertAt', 'interleave', 'isEmpty',
'iter', 'kernel', 'last', 'length',
'iter', 'last', 'length',
'map', 'maxBy', 'minBy',
'pairwise', 'partition', 'push',
'reduce', 'reduceBack', 'replicate',
Expand All @@ -32,8 +32,8 @@ describe('array', () => {
'sortByDescending', 'sortDescending', 'sortInPlace',
'sortInPlace', 'sortInPlaceBy', 'sortInPlaceWith',
'sortInPlaceWith', 'sortWith', 'sortWith',
'splitAt', 'splitInto', 'stride', 'sum',
'sumBy', 'remove', 'tail', 'take',
'splitAt', 'splitInto', 'stride', 'strideWindowed',
'sum', 'sumBy', 'remove', 'tail', 'take',
'takeWhile', 'takeWhileInclusive', 'transpose',
'truncate', 'tryFind', 'tryFindBack',
'tryFindIndex', 'tryFindIndexBack', 'tryHead',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import {expect} from 'chai';

import {checkThrow} from '../../../support/checkThrow';
import {chunkBySize, kernel, range, windowed} from '../../../../src/array';
import {chunkBySize, strideWindowed, range, windowed} from '../../../../src/array';

describe('kernel', () => {
describe('strideWindowed', () => {

it('should kernel 1:2', () => {
it('should strideWindowed 1:2', () => {
const array = [1, 2, 3, 4];
const fn = kernel(2)(2);
const fn = strideWindowed(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', () => {
it('should strideWindowed 2:3', () => {
const array = [1, 2, 3, 4, 5, 6, 7];
const fn = kernel(2)(3);
const fn = strideWindowed(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([]);
Expand All @@ -23,41 +23,41 @@ describe('kernel', () => {
describe('iterate', () => {
for (let i = 1; i < 50; i += Math.ceil(Math.random() * 5)) {

it(`should kernel 1:${i} equals windowed ${i}`, () => {
it(`should strideWindowed 1:${i} equals windowed ${i}`, () => {
const array = range(0, 100);
const fn = kernel(1)(i);
const fn = strideWindowed(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}`, () => {
it(`should strideWindowed ${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);
const fn = strideWindowed(i)(i);
expect(fn(array)).to.eql(chunkBySize(i)(array));
});
}
});

it('should return empty if window greater than length', () => {
const fn = kernel(1)(2);
const fn = strideWindowed(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)`);
expect(() => strideWindowed(1)(0)).to.throw(`windowSize must not be less than 1. (0 given)`);
expect(() => strideWindowed(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)`);
expect(() => strideWindowed(0)(1)).to.throw(`stride must not be less than 1. (0 given)`);
expect(() => strideWindowed(-1)(1)).to.throw(`stride must not be less than 1. (-1 given)`);
});


it('should return empty', () => {
const fn = kernel(1)(2);
const fn = strideWindowed(1)(2);
expect(fn([])).to.eql([]);
});

it('should throw if null or undefined', () => {
const fn = kernel(1)(2);
const fn = strideWindowed(1)(2);
checkThrow(fn);
});
});
2 changes: 1 addition & 1 deletion src/array/operator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export * from './insertAt';
export * from './interleave';
export * from './isEmpty';
export * from './iter';
export * from './kernel';
export * from './last';
export * from './length';
export * from './map';
Expand Down Expand Up @@ -63,6 +62,7 @@ export * from './sortWith';
export * from './splitAt';
export * from './splitInto';
export * from './stride';
export * from './strideWindowed';
export * from './sum';
export * from './sumBy';
export * from './tail';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const kernel = (stride: number) => {
export const strideWindowed = (stride: number) => {

if (stride < 1) {
throw Error(`stride must not be less than 1. (${stride} given)`);
Expand Down
4 changes: 2 additions & 2 deletions src/array/operator/windowed.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {kernel} from './kernel';
import {strideWindowed} from './strideWindowed';

export const windowed = kernel(1)
export const windowed = strideWindowed(1)

0 comments on commit 702df77

Please sign in to comment.