Scala List.filter with two conditions, applied only once

前端 未结 4 361
旧时难觅i
旧时难觅i 2021-02-04 13:18

Don\'t know if this is possible, but I have some code like this:

val list = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
val evens = list.filter { e => e % 2 =         


        
4条回答
  •  隐瞒了意图╮
    2021-02-04 14:05

    Wouldn't this work:

    list.filter{e => e % 2 == 0 && (if (someCondition) e % 3 == 0 else e % 5 == 0)}
    

    also FYI e % 2 == 0 is going to give you all the even numbers, unless you're naming the val odds for another reason.

提交回复
热议问题