Skip to content

Instantly share code, notes, and snippets.

@ichpuchtli
Forked from samgiles/flatMap.js
Last active February 22, 2016 13:19
Show Gist options
  • Save ichpuchtli/1c7026101b0e506bc023 to your computer and use it in GitHub Desktop.
Save ichpuchtli/1c7026101b0e506bc023 to your computer and use it in GitHub Desktop.
Javascript flatMap implementation
// [B](f: (A) ⇒ [B]): [B] ; Although the types in the arrays aren't strict (:
function flatMap<T, U>(array: T[], mapFunc: (x: T) => U[]) : U[] {
return array.reduce((cumulus: U[], next: T) => [...mapFunc(next), ...cumulus], <U[]> []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment