Combining elements in a Scala list -


how combine elements in list, e.g.

list(('h', 1), ('i', 1), ('h', 1), ('i', 1), ('l', 2)) 

such following result:

list(('h', 2), ('i', 2), ('l', 2)) 

basically, want sum numbers associated each letter, , letter should appear in list once.

val mylist = list(('h', 1), ('i', 3), ('h', 5), ('i', 7), ('l', 2)) mylist.groupby(_._1).mapvalues(_.foldleft(0)(_ + _._2)).tolist res0: list[(char, int)] = list((h,6), (i,10), (l,2)) 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -