简体   繁体   English

在Clojure中,如何选择并返回集合的子集

[英]In clojure, how can I select and return a subset of a collection

If I have a collection like this 如果我有这样的收藏

{{:A "a" :B "b"} {:A "a" :B "Goal" } {:A "a" :B "Goal"}}

how do I make a function so it returns only the parts where :B "Goal" ? 我该如何做一个函数,使其仅返回:B "Goal" Meaning only: 仅含义:

{{:A "a" :B "Goal" } {:A "a" :B "Goal"}}

I fixed your data to make it a vector of 3 maps. 我修正了您的数据,使其成为3张地图的向量。

(def data [{:A "a" :B "b"}
           {:A "a" :B "Goal"}
           {:A "a" :B "Goal"}])

(defn b-goal?
  [item]
  (= (:B item) "Goal") )

(filter b-goal? data) => ({:A "a", :B "Goal"} {:A "a", :B "Goal"})

Be sure also to check out: 还要确保签出:

As well as books like 以及像

  • Living Clojure 活着的Clojure
  • Getting Clojure 获取Clojure

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将多维数组作为JUnit测试的参数集合返回? - How can I return a multidimensional array as a collection of parameters for a JUnit test? 在clojure中,您可以将函数应用于集合中的所有元素,并让返回值作为下一个的输入吗? - In clojure can you apply a function to all elements in a collection and have the return value be the input for the next? 你如何归还收藏品 <ConcreteType> 作为一个集合 <Interface> ? - How can you return a Collection<ConcreteType> as a Collection<Interface>? 我怎样才能获得一系列的收藏品 - How can I get a collection of collection 如何对具有Collection返回值的Collection参数进行单元测试? - How do I unit test for a Collection parameter with an Collection return? 我怎样才能收集一个集合 - How can I cast a collection 如何构建具有多维通配符索引的集合类型以返回多个匹配? - How can I build a collection type with multidimensional wildcard indexing to return multiple matches? 使用Ruby,如何将集合传递给方法,但只是方法的一部分? - Using Ruby, How to pass collection to method, but a subset of it? 如何使用 jQuery 从多个集合 select 中提取 id? - How can I use jQuery to pull the ids from a multiple collection select? 我可以使用foreach仅从集合中返回某种类型吗? - Can I use foreach to return only a certain type from a collection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM