扁平映射

将函数映射到集合上,并将结果扁平化一层
通常,您希望在返回列表中多个值的输入列表上映射函数,但您不希望输出像输入一样嵌套。
ruby…
["two birds", "three green peas"].
flat_map {|s| s.split}
# => ["two", "birds", "three", "green", "peas"]
clojure…
(mapcat #(clojure.string/split % #"\s+")
["two birds" "three green peas"])
;; => ("two" "birds" "three" "green" "peas")
["two birds", "three green peas"].
map {|s| s.split}.
flatten (1)
# => ["two", "birds", "three", "green", "peas"]
但它非常常用,以至于许多平台都提供 flat-map 操作。
您也可以将其视为将 map 的所有结果拼接在一起 - 因此 clojure 的名称为“mapcat”。
clojure…(apply concat (map #(clojure.string/split % #"\s+")
["two birds" "three green peas"]))
;; => ("two" "birds" "three" "green" "peas")
