Scala - can a lambda parameter match a tuple?

前端 未结 5 1646
眼角桃花
眼角桃花 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条回答
  •  梦毁少年i
    2021-02-02 06:36

    This is about the closest you can get:

     val m = l.filter { case (n, s) => n != 2 }
    

    It's basically pattern matching syntax inside an anonymous PartialFunction. There are also the tupled methods in Function object and traits, but they are just a wrapper around this pattern matching expression.

提交回复
热议问题