[英]Scala get all elements in a list less than a certain value
I'm trying to get a partition of a list based on the values less than some parameter I'm passing into the function. 我试图基于小于传递给函数的某些参数的值来获取列表的分区。 I'm thinking of using the map function somehow to apply a function in order to make this new list, but I don't see how to do so:
我正在考虑使用map函数以某种方式应用函数以创建此新列表,但是我不知道如何这样做:
exampleList.map(s => s<10)
For instance here, I want to get all the elements of the list that are less than 10, but I feel like this would just return a list of booleans. 例如,在这里,我想获取列表中小于10的所有元素,但是我觉得这只会返回一个布尔值列表。 I know I can also use for comprehension with yield or maybe reduce, but I'm unsure of how to do so.
我知道我也可以用收益率或降低收益来理解,但是我不确定该怎么做。 (My Scala knowledge is limited)
(我的Scala知识有限)
Thanks in advance for any help 预先感谢您的任何帮助
Use the filter
method: 使用
filter
方法:
exampleList.filter(s => s < 10)
Using lambda syntactic sugar: 使用lambda语法糖:
exampleList.filter(_ < 10)
Using list comprehensions 使用列表推导
for (s <- exampleList; if s < 10) yield s
The Seq
API is a good place to start if you want to expand your knowledge of the collection API: 如果您想扩展集合API的知识,那么
Seq
API是一个不错的起点:
http://www.scala-lang.org/api/current/index.html#scala.collection.Seq http://www.scala-lang.org/api/current/index.html#scala.collection.Seq
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.