简体   繁体   中英

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:

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. 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)

Thanks in advance for any help

Use the filter method:

exampleList.filter(s => s < 10)

Using lambda syntactic sugar:

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:

http://www.scala-lang.org/api/current/index.html#scala.collection.Seq

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM