Created
January 19, 2017 10:47
-
-
Save ruanyf/d7141535fe0442ab7766d63617484160 to your computer and use it in GitHub Desktop.
map vs. flatMap
This file contains 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
const map = (array, func) => ( | |
array.map(func) | |
); | |
const flatMap = (array, func) => ( | |
array.reduce((result, element) => ( | |
result.concat(func(element)) | |
), []) | |
); | |
const arr = ['two birds', 'three green peas']; | |
const split = s => s.split(' '); | |
map(arr, split) | |
// [["two", "birds"], ["three", "green", "peas"]] | |
flatMap(arr, split) | |
// ["two", "birds", "three", "green", "peas"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment