use flat_map instead of map.flatten
Merge pull request #589 from arthurnn/flat_map

use flat_map instead of map.flatten