What will be the result of executing foldMap on List of Ints with toString function?
val l = List(1, 2, 3, 4, 5)

l.foldMap(i => i.toString)
foldMap maps elements using provided function - in current example toString; then combines elements using Monoid of the result type: Monoid[String]. If only mapping was performed, the result would be List("1", "2", "3", "4", "5"). Performing combine results in String concatenation, which is "12345".

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Scala quizzes
Sign Up Now
or Subscribe for future quizzes