foldLeft method in scala -
i using following code count of words in text
.foldleft(map.empty[string, float]){ (countssource, wordsource) => countssource + (wordsource -> (countssource.getorelse(wordsource, 0) + 1))} i getting error 1 should string. if put in quotes, says should float.
what doing wrong?? in advance
getorelse's signature follows:
def getorelse[b1 >: b](key: a, default: => b1): b1 which means doesn't return type of map's value (b), potentially superclass b1. in case, using literal 0 doesn't inform compiler you're interested in float.
using 0f instead fix it:
l.foldleft(map.empty[string, float]){ (countssource, wordsource) => countssource + (wordsource -> (countssource.getorelse(wordsource, 0f) + 1))}
Comments
Post a Comment