![](/img/trans.png)
[英]How to sum the elements of a list if the difference of elements in another list is less than a certain value
[英]Scala get all elements in a list less than a certain value
我試圖基於小於傳遞給函數的某些參數的值來獲取列表的分區。 我正在考慮使用map函數以某種方式應用函數以創建此新列表,但是我不知道如何這樣做:
exampleList.map(s => s<10)
例如,在這里,我想獲取列表中小於10的所有元素,但是我覺得這只會返回一個布爾值列表。 我知道我也可以用收益率或降低收益來理解,但是我不確定該怎么做。 (我的Scala知識有限)
預先感謝您的任何幫助
使用filter
方法:
exampleList.filter(s => s < 10)
使用lambda語法糖:
exampleList.filter(_ < 10)
使用列表推導
for (s <- exampleList; if s < 10) yield s
如果您想擴展集合API的知識,那么Seq
API是一個不錯的起點:
http://www.scala-lang.org/api/current/index.html#scala.collection.Seq
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.