Scala - can a lambda parameter match a tuple?

前端 未结 5 1731
眼角桃花
眼角桃花 2021-02-02 05:42

So say i have some list like

val l = List((1, \"blue\"), (5, \"red\"), (2, \"green\"))

And then i want to filter one of them out, i can do some

5条回答
  •  误落风尘
    2021-02-02 06:15

    There are a bunch of options:

    for (x <- l; (n,s) = x if (n != 2)) yield x
    l.collect{ case x @ (n,s) if (n != 2) => x }
    l.filter{ case (n,s) => n != 2 }
    l.unzip.zipped.map((n,s) => n != 2).zip   // Complains that zip is deprecated
    

提交回复
热议问题