Skip to content

Instantly share code, notes, and snippets.

View holgergp's full-sized avatar

Holger Grosse-Plankermann holgergp

View GitHub Profile
@holgergp
holgergp / flatMapTest.sc
Last active December 15, 2020 13:45
Some fiddling with map, flatMap and foldLeft on Collections, Strings and Option. Trying to understand the generic monadic approach
object flatMapTest {
import scala.util.{ Try, Success, Failure }
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
val intList = 1 :: 2 :: 5 :: Nil //> intList : List[Int] = List(1, 2, 5)
intList.map(x => x * 2) //> res0: List[Int] = List(2, 4, 10)
//intList.flatMap(x=>x*2) //type mismatch; found : Int required: scala.collection.GenTraversableOnce[?]
intList.foldLeft(0)((x, y) => x + y) //> res1: Int = 8
val stringList = "eins" :: "zwei" :: "drei" :: Nil