Skip to content

Commit

Permalink
add array/stride
Browse files Browse the repository at this point in the history
  • Loading branch information
mo-ba committed Oct 21, 2023
1 parent 31cb2d3 commit a28bae0
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/src/config-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
37 changes: 37 additions & 0 deletions docs/src/pages/en/array/operator/stride.md
Original file line number Diff line number Diff line change
@@ -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) => <E>(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)
2 changes: 1 addition & 1 deletion spec/core/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
18 changes: 17 additions & 1 deletion spec/core/array.timing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,21 @@ describe('performance test', function () {

}).timeout(oneMinute);
});
describe('stride', () => {
it('stride', () => {
const stride = <T>(step: number) => <T>(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 = <T>(predicate: Predicate<T>) => (array: T[]): T[] => {
Expand Down Expand Up @@ -415,5 +430,6 @@ describe('performance test', function () {
}).timeout(oneMinute);


});
})
;

31 changes: 31 additions & 0 deletions spec/core/array/operator/stride.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
2 changes: 2 additions & 0 deletions src/array/operator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
7 changes: 7 additions & 0 deletions src/array/operator/stride.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const stride = (step: number) => <E>(array: E[]): E[] => {
const result = [];
for (let i = 0; i < array.length; i += step) {
result.push(array[i]);
}
return result;
};

0 comments on commit a28bae0

Please sign in to comment.