MyList - step 6
For Comprehension
The MyList has a map and flatMap function
So we can use in a for comprehension
Exercise
@main
def main(): Unit =
val result = for
a <- MyList(1, 2, 3)
b <- MyList(a, a + 1)
yield
b
println(result)
This is the same as the example in the for
@main
def main(): Unit =
val result = for
a <- MyList(1, 2, 3)
b <- MyList(a, a + 1)
yield
b
println(result)
WithFilter
def withFilter[A](f: A => MyList[B]): MyList[B]
In the scala for comprehension we can filter elements.
This is not the filter method but the withFilter method.
In the standard libraries the withFilter is lazy, but that is for the advanced course
So for now we use our filter method as implementation
def withFilter[A](f: A => MyList[B]): MyList[B] = filter(f)
@main
def main(): Unit =
val result = for
a <- MyList(1, 2, 3, 4, 5) if a < 3
yield
a
println(result)