我试图从散列向量中检索整个散列,这取决于它在字段中是否有特定的值。
(def foo {:a 1, :b 2})
(def bar {:a 3, :b 4})
(def baz [foo bar])在baz中,我希望返回:a 3所在的整个散列,因此结果将是{:a 3, :b 4}。我已经尝试过get、get-in和find,但是它们依赖于键而不返回整个哈希。我也尝试过来自this question的一些建议,但他们也不返回哈希。
发布于 2015-11-24 19:42:05
过滤到救援!
hello.core> (def foo {:a 1, :b 2})
#'hello.core/foo
hello.core> (def bar {:a 3, :b 4})
#'hello.core/bar
hello.core> (def baz [foo bar])
#'hello.core/baz
hello.core> (filter #(= (:a %) 3) baz)
({:a 3, :b 4})#(= (:a %) 3)是一个创建匿名的简短形式,它接受一个名为%的参数,其中它将查找键:a,如果匹配值为3,则返回true。向量baz中通过测试的任何条目都会将其输入到输出中。
PS:关于发音的注意事项:该数据结构通常称为"map“,因为它将一个键映射到一个值。这非常令人困惑,因为还有一个名为map的函数,它通过函数更改序列的每个成员。
发布于 2015-11-24 22:54:56
正如亚瑟所提到的,filter绝对做好了这项工作。为了完整起见,这是另外两个解决方案,它们在两个方面与filter不同
(some #(when (= 3 (:a %)) %) baz)
(first (drop-while #(not= 3 (:a %)) baz))https://stackoverflow.com/questions/33902263
复制相似问题