interface Tuple {
first: A;
second: B;
}
/*
Create a new tuple
*/
export function pair(first: A, second: B): Tuple {
return {
first: first,
second: second,
};
}
/*
First element of tuple
*/
export function first(tuple: Tuple): A {
return tuple.first;
}
/*
Second element of tuple
*/
export function second(tuple: Tuple): B {
return tuple.second;
}
/*
Apply a function to the first element of a tuple and save it
*/
export function mapFirst(
func: (val: A) => X,
tuple: Tuple
): Tuple {
return {
first: func(tuple.first),
second: tuple.second,
};
}
/*
Apply a function to the second element of a tuple and save it
*/
export function mapSecond(
func: (val: B) => X,
tuple: Tuple
): Tuple {
return {
first: tuple.first,
second: func(tuple.second),
};
}
/*
Apply two separate functions to the first and second elements of a tuple and save it
*/
export function mapBoth(
firstFunc: (val: A) => X,
secondFunc: (val: B) => Y,
tuple: Tuple
): Tuple {
return {
first: firstFunc(tuple.first),
second: secondFunc(tuple.second),
};
}