import { Arity2, curry } from '@typed/lambda'
/**
* Combine a list of lists together by applying a function to
* the values contained in a list.
* @param fn :: (a -> [b])
* @param list :: [a]
* @returns :: [b]
*/
export const chain = curry((f, list) => unnest(list.map(f))) as {
(fn: Arity2>, list: ReadonlyArray): B[]
(fn: Arity2>): (list: ReadonlyArray) => B[]
}
/**
* Flatten a list of lists with a depth of 1.
* @param nestedList :: [[a]]
* @returns:: [a]
*/
export function unnest(nestedList: ReadonlyArray>): A[] {
const unnestedList: A[] = []
for (const list of nestedList) {
unnestedList.push(...list)
}
return unnestedList
}