-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathzip.ts
28 lines (22 loc) · 907 Bytes
/
zip.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
// Copyright (c) 2020 Jozty. All rights reserved. MIT license.
import { zipWith } from './zipWith.ts';
import curryN from './utils/curry_n.ts';
import type { PH } from './utils/types.ts';
// @types
type Zip_2<T1> = <T2>(list2: T2[]) => [T1, T2][];
type Zip_1<T2> = <T1>(list1: T1[]) => [T1, T2][];
type Zip =
& (<T1>(list1: T1[]) => Zip_2<T1>)
& (<T2>(list1: PH, list2: T2[]) => Zip_1<T2>)
& (<T1, T2>(list1: T1[], list2: T2[]) => [T1, T2][]);
function _zip<T1, T2>(list1: T1[], list2: T2[]): [T1, T2][] {
return zipWith((a, b) => [a, b], list1, list2);
}
/**
* Creates a new list out of two passed lists `list1`, `list2` by pairing up
* equally-positioned pair in both the lists.
* The returned is truncated to the length of the shorter of the two input lists.
*
* Fae.zip([100, 200, 300], [1, 2, 3]) // [[1, 100], [2, 200], [3, 300]]
*/
export const zip = curryN(2, _zip) as Zip;