[英]Clojure: Idiomatic way to wrap each element in collection
If I have a collection of a particular shape: 如果我有特定形状的集合:
["Alpha", "Beta", "Gamma"] ;; vector of strings
and I want to transform it by wrapping each element: 我想通过包装每个元素来对其进行转换:
[{:name "Alpha"}, {:name "Beta"}, {:name "Gamma"}]
Is there a better way to express that than this rather kludgy map
? 有比这张模糊的
map
更好的表达方式吗?
(map #(identity {:name %}) coll)
If you don't like map
with (fn [v] {:name v})
you can use for
: 如果您不喜欢使用
(fn [v] {:name v})
map
,则可以for
:
(for [v coll] {:name v})
;; => ({:name "Alpha"} {:name "Beta"} {:name "Gamma"})
You could simply use fn
: 您可以简单地使用
fn
:
(map (fn [v] {:name v}) coll)
if you want to use the anonymous function syntax you can use array-map
to construct the map: 如果要使用匿名函数语法,则可以使用
array-map
构造映射:
(map #(array-map :name %) coll)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.