For example, suppose I have
val list: List[(String, Double)]
with values
"04-03-1985", 1.5
"05-03-1985", 2.4
"05-03-1985", 1.3
How could I produce a new List
"04-03-1985", 1.5
"05-03-1985", 3.7
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here’s a one-liner. It’s not particularly readable, unless one really internalizes the types of these higher order functions.
Another approach is to add the key-value pairs one-by-one using fold,
The equivalent for comprehension is most accessible, in my opinion,
Maybe something nicer can be done with Scalaz’s monoid typeclass for Map.
Note that you can convert between
Map[K, V]andSeq[(K, V)]using thetoSeqandtoMapmethods.Update. After pondering it some more, I think the natural abstraction would be a “multimap” conversion, of type,
With the appropriate implicit extension in one’s personal library, one could then write:
This is the clearest of all, in my opinion!