Created
December 12, 2015 18:04
-
-
Save wmaurer/9a13e01299b145e22929 to your computer and use it in GitHub Desktop.
flatMap function on Array.prototype
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Array<T> { | |
flatMap<TResult>(selector: (source: T) => Array<TResult>): Array<TResult>; | |
} | |
Array.prototype.flatMap = function<T, TResult>(selector: (source: T) => Array<TResult>) { | |
return (this as Array<T>).map(value => selector(value)).reduce((acc, value) => { | |
return acc.concat(value); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment