Skip to content

Instantly share code, notes, and snippets.

@codejockie
Forked from samgiles/flatMap.js
Created April 8, 2018 19:09
Show Gist options
  • Save codejockie/f65f958051c3641ce8cfa9c77803e530 to your computer and use it in GitHub Desktop.
Save codejockie/f65f958051c3641ce8cfa9c77803e530 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 (:
Array.prototype.flatMap = function(lambda) {
return Array.prototype.concat.apply([], this.map(lambda));
};
@codejockie
Copy link
Author

function flatMap<T, U>(array: T[], callbackfn: (value: T, index: number, array: T[]) => U[]): U[] {
    return [].concat(...array.map(callbackfn));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment