I don't have the book, but following the code from here, I can point out a few things for you to think about( based on some println()s in a REPL session ):
As S.R.I pointed out, you have a list of phrases, not words. The functions on github don't do anything to separate the word groups for you, so at best, your current input Vector could get:
Map(a rose -> 2, is a -> 1, rose is -> 1)
You could create a list of words by doing the following to your Vector:
Vector("a rose", "is a", "rose is", "a rose") map( _.split( " " ).toSeq ) flatten
The
mapMergeMonoid
function only appears to add the value of k and v together when there keys(k) are the same. Meaning an unsorted list is going to result in a lot ofMap(string -> 1)
.You could sort the Vector of words by making the following changes to Vector:
(Vector("a rose", "is a", "rose is", "a rose") map( _.split( " " ).toSeq ) flatten) sortWith(_.compareTo(_) < 0)
While
foldMapV
does split out all the phrases or words from the Vector in aMap[String, Int]
, it only appears to return the leftmost desired merge for a sorted IndexedSeq. With a sorted Vector of words as I've indicated, I got the resultMap(a -> 3)
from your implementation offrequencyMap
. I would be inclined to use something other thanfoldMapV
though or modify it to makefrequencyMap
work. The bit that appears to be missing to me is an accumulation of the results of the function it's applying into one large Map. I would also try a more "standard" head/tail recursion than the splitAt() calls(as I'm not convinced thefoldMapV
was handlingis
androse
very well).