Skip to content

Instantly share code, notes, and snippets.

@ajcrites
Created April 25, 2018 22:10
Show Gist options
  • Save ajcrites/f705bc60e88077984dd6036716fb8df2 to your computer and use it in GitHub Desktop.
Save ajcrites/f705bc60e88077984dd6036716fb8df2 to your computer and use it in GitHub Desktop.
export interface Foldable<F, A> {
reduce: (fn: (b: A, a: A) => A, initial: A, foldable: F) => A;
}
export const getArrayFold = <R, Q extends Array<R> = Array<R>>(): Foldable<Q, R> => {
return {
reduce: (fn, initial, array) => {
return array.reduce(fn, initial);
}
};
};
getArrayFold<string>().reduce(
(a, b) => a + b,
'',
['hello', 'world']
);
getArrayFold<number>().reduce(
(a, b) => a + b,
0,
[1, 2, 3]
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment