From a28bae0d47aa89a17e44e2e4ef75ee239d733c35 Mon Sep 17 00:00:00 2001 From: mb Date: Sat, 21 Oct 2023 11:05:10 +0200 Subject: [PATCH] add array/stride --- docs/src/config-menu.ts | 4 +++ docs/src/pages/en/array/operator/stride.md | 37 ++++++++++++++++++++++ spec/core/array.spec.ts | 2 +- spec/core/array.timing.ts | 18 ++++++++++- spec/core/array/operator/stride.spec.ts | 31 ++++++++++++++++++ src/array/operator/index.ts | 2 ++ src/array/operator/stride.ts | 7 ++++ 7 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 docs/src/pages/en/array/operator/stride.md create mode 100644 spec/core/array/operator/stride.spec.ts create mode 100644 src/array/operator/stride.ts diff --git a/docs/src/config-menu.ts b/docs/src/config-menu.ts index bec0b20..0718f86 100644 --- a/docs/src/config-menu.ts +++ b/docs/src/config-menu.ts @@ -142,6 +142,10 @@ export default { "text": "splitInto", "link": "en/array/operator/splitInto" }, + { + "text": "stride", + "link": "en/array/operator/stride" + }, { "text": "tail", "link": "en/array/operator/tail" diff --git a/docs/src/pages/en/array/operator/stride.md b/docs/src/pages/en/array/operator/stride.md new file mode 100644 index 0000000..1ea8ba8 --- /dev/null +++ b/docs/src/pages/en/array/operator/stride.md @@ -0,0 +1,37 @@ +--- +title: stride +description: stride +layout: ../../../../layouts/MainLayout.astro +--- +Creates a new array by taking every nth element from the input array. + +## Type + +```ts +type stride = (step: number) => (array: E[]) => E[] +``` + + +## Parameters + +- `step` (number): The step size for selecting elements from the input array. +- `array` (Array): An array of elements from which to select elements. + +## Returns + +- (Array): A new array containing every nth element from the input array. + +## Example + +```ts +import {stride} from 'fnxt/array'; + +const array = [1, 2, 3, 4, 5, 6, 7]; +const stride = stride(3); +stride(array) // -> [1, 4, 7] +``` + +## See Also + +- [skip](./skip) +- [take](./take) diff --git a/spec/core/array.spec.ts b/spec/core/array.spec.ts index 23b6458..58bf824 100644 --- a/spec/core/array.spec.ts +++ b/spec/core/array.spec.ts @@ -32,7 +32,7 @@ describe('array', () => { 'sortByDescending', 'sortDescending', 'sortInPlace', 'sortInPlace', 'sortInPlaceBy', 'sortInPlaceWith', 'sortInPlaceWith', 'sortWith', 'sortWith', - 'splitAt', 'splitInto', 'sum', + 'splitAt', 'splitInto', 'stride', 'sum', 'sumBy', 'remove', 'tail', 'take', 'takeWhile', 'takeWhileInclusive', 'transpose', 'truncate', 'tryFind', 'tryFindBack', diff --git a/spec/core/array.timing.ts b/spec/core/array.timing.ts index cb7b9d8..6cac76e 100644 --- a/spec/core/array.timing.ts +++ b/spec/core/array.timing.ts @@ -210,6 +210,21 @@ describe('performance test', function () { }).timeout(oneMinute); }); + describe('stride', () => { + it('stride', () => { + const stride = (step: number) => (array: T[]): T[] => + array.filter((_, i) => i % step == 0); + + const data = ARRAY.range(0, length); + + const step = 13; + const fnxt = ARRAY.stride(step); + const alternative = stride(step); + + run(fnxt, alternative, data); + + }).timeout(oneMinute); + }); it('skipWhile', () => { const skipWhile = (predicate: Predicate) => (array: T[]): T[] => { @@ -415,5 +430,6 @@ describe('performance test', function () { }).timeout(oneMinute); -}); +}) +; diff --git a/spec/core/array/operator/stride.spec.ts b/spec/core/array/operator/stride.spec.ts new file mode 100644 index 0000000..808e60b --- /dev/null +++ b/spec/core/array/operator/stride.spec.ts @@ -0,0 +1,31 @@ +import {expect} from 'chai'; +import {checkThrow} from '../../../support/checkThrow'; +import {stride} from '../../../../src/array'; + +describe('stride', () => { + + it('should stride 1', () => { + const fn = stride(1); + expect(fn([1, 2, 3, 4, 5, 6])).to.eql([1, 2, 3, 4, 5, 6],); + }); + + it('should stride 2', () => { + const fn = stride(2); + expect(fn([1, 2, 3, 4, 5, 6])).to.eql([1, 3, 5],); + }); + + it('should stride 3', () => { + const fn = stride(3); + expect(fn([1, 2, 3, 4])).to.eql([1, 4],); + }); + + it('should stride empty', () => { + const fn = stride(2); + expect(fn([])).to.eql([]); + }); + + it('should throw if null or undefined', () => { + const fn = stride(1); + checkThrow(fn); + }); +}); diff --git a/src/array/operator/index.ts b/src/array/operator/index.ts index 9fa951b..bee7472 100644 --- a/src/array/operator/index.ts +++ b/src/array/operator/index.ts @@ -53,6 +53,8 @@ export * from './skipWhile'; export * from './takeWhile'; export * from './takeWhileInclusive'; +export * from './stride'; + export * from './where'; export * from './exists'; export * from './some'; diff --git a/src/array/operator/stride.ts b/src/array/operator/stride.ts new file mode 100644 index 0000000..70025d9 --- /dev/null +++ b/src/array/operator/stride.ts @@ -0,0 +1,7 @@ +export const stride = (step: number) => (array: E[]): E[] => { + const result = []; + for (let i = 0; i < array.length; i += step) { + result.push(array[i]); + } + return result; +};