In below example I'm attempting to return smallest 2 elements (nearest neighbours) for which "a" is a member of.
So smallest two elements for "a" based on :
List((("a","b"),1.0) , (("a","c"),4.0) , (("a","c"),3.0) , (("b","c"),2.0) )
is
List((("a","b"),1.0) , (("a","c"),3.0))
Here is my solution :
val l = List((("a","b"),1.0) , (("a","c"),4.0) , (("a","c"),3.0) , (("b","c"),2.0) )
//> l : List[((String, String), Double)] = List(((a,b),1.0), ((a,c),4.0), ((a,c
//| ),3.0), ((b,c),2.0))
val justA = l.filter(v => v._1._1.equals("a") || v._1._2.equals("a")).sortBy(_._2).take(2)
//> justA : List[((String, String), Double)] = List(((a,b),1.0), ((a,c),3.0))
Is there a more efficient solution to this calculation ?