I thought the same thing and came to you today.
I don't really like partial function approaches (anything having a case ), as they imply that there may be more entry points for the logical stream. At least for me they tend to blur the intention of the code. On the other hand, I really want to go straight to the fields of the tuple, for example, to you.
Here is the solution I developed today. This seems to work, but I haven't tried it in production yet.
object unTuple { def apply[A, B, X](f: (A, B) => X): (Tuple2[A, B] => X) = { (t: Tuple2[A, B]) => f(t._1, t._2) } def apply[A, B, C, X](f: (A, B, C) => X): (Tuple3[A, B, C] => X) = { (t: Tuple3[A, B, C]) => f(t._1, t._2, t._3) } //... } val list = List( ("a",1), ("b",2) ) val list2 = List( ("a",1,true), ("b",2,false) ) list foreach unTuple( (k: String, v: Int) => println(k, v) ) list2 foreach unTuple( (k: String, v: Int, b: Boolean) => println(k, v, b) )
Output:
(a,1) (b,2) (a,1,true) (b,2,false)
It may be useful. The unTuple object unTuple naturally be set aside in some tool namespace.
Application:
Applicable to your case:
val m = l.filter( unTuple( (n:Int,color:String) => n != 2 ))