-
Notifications
You must be signed in to change notification settings - Fork 7
/
aperture.ts
37 lines (28 loc) · 1.05 KB
/
aperture.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright (c) 2020 Jozty. All rights reserved. MIT license.
import { dispatch } from './utils/dispatch.ts';
import ApertureTransformer from './utils/Transformers/aperture.ts';
import curryN from './utils/curry_n.ts';
import type { PH } from './utils/types.ts';
// @types
type Aperture_2 = <T>(list: T[]) => T[][];
type Aperture_1<T> = (n: number) => T[][];
type Aperture =
& ((n: number) => Aperture_2)
& (<T>(n: PH, list: T[]) => Aperture_1<T>)
& (<T>(n: number, list: T[]) => T[][]);
function _aperture<T>(n: number, list: T[]) {
const len = list.length - n + 1;
const result = new Array(Math.max(0, len));
for (let i = 0; i < len; i++) {
result[i] = list.slice(i, i + n);
}
return result;
}
const dispatched = dispatch(ApertureTransformer, _aperture);
/**
* Returns a new list, composed of n-tuples of consecutive elements. If `n` is
* greater than the length of the list, an empty list is returned.
*
* Acts as a transducer if a transformer is passed in place of `list`
*/
export const aperture = curryN(2, dispatched) as Aperture;