UNPKG

885 BJavaScriptView Raw
1/**
2 * Working with value pairs.
3 *
4 * @module pair
5 */
6
7/**
8 * @template L,R
9 */
10export class Pair {
11 /**
12 * @param {L} left
13 * @param {R} right
14 */
15 constructor (left, right) {
16 this.left = left
17 this.right = right
18 }
19}
20
21/**
22 * @template L,R
23 * @param {L} left
24 * @param {R} right
25 * @return {Pair<L,R>}
26 */
27export const create = (left, right) => new Pair(left, right)
28
29/**
30 * @template L,R
31 * @param {R} right
32 * @param {L} left
33 * @return {Pair<L,R>}
34 */
35export const createReversed = (right, left) => new Pair(left, right)
36
37/**
38 * @template L,R
39 * @param {Array<Pair<L,R>>} arr
40 * @param {function(L, R):any} f
41 */
42export const forEach = (arr, f) => arr.forEach(p => f(p.left, p.right))
43
44/**
45 * @template L,R,X
46 * @param {Array<Pair<L,R>>} arr
47 * @param {function(L, R):X} f
48 * @return {Array<X>}
49 */
50export const map = (arr, f) => arr.map(p => f(p.left, p.right))