This is a motivational example, Given:
List((1,2), (2,1), (3,1))
I'd like to return:
List((1,2),(3,1))
I've tried to do this in several ways. First:
List((1,2), (2,1), (3,1)) map { case (a,b) => if (a > b) (a,b) else (b,a) }
distinct
Then I tried to use a tuple:
List((1,2), (3,4), (2,1)) map { t => if (t._1 <= t._2) t else t.swap }
then defining the partial function a little differently:
val pf: PartialFunction[(Int,Int), (Int,Int)] = {
case (i, j) if i >= j => (j, i)
}
List((1,2), (3,4), (2,1)) map pf distinct
Is there a way that apply the PartialFunction only to the elementes that is defined for? or compound the PF with Identity in some how.