scala - ReduceLeft with Vector of pairs? -
i have vector of pairs
vector((9,1), (16,2), (21,3), (24,4), (25,5), (24,6), (21,7), (16,8), (9,9), (0,10))
and want return pair maximum first element in pair. i've tried this:
data reduceleft[(int, int)]((y:(int, int),z:(int,int))=>y._1 max z._1)
and
data reduceleft((y:(int, int),z:(int,int))=>y._1 max z._1)
but there type mismatch error , can't understand wrong code.
why using reduceleft ? default max method works well
scala> val v = vector((9,1), (16,2), (21,3), (24,4), (25,5), (24,6), (21,7), (16,8), (9,9), (0,10)) v: scala.collection.immutable.vector[(int, int)] = vector((9,1), (16,2), (21,3), (24,4), (25,5), (24,6), (21,7), (16,8), (9,9), (0,10)) scala> v.max res1: (int, int) = (25,5)
if want reduceleft instead :
v.reduceleft( (x, y) => if (x._1 >= y._1) x else y )
your error have return tuple, not int
y._1 max z._1
the max function here on 2 int return int.
Comments
Post a Comment