MyList - step 4
Foreach, Map and Filter
Exercise
Add the following methods to MyList
- foreach
- map
- filter
trait MyList[A]:
...
def foreach(f: A => B): Unit
def map[B](f: A => B): MyList[B]
def filter(f: A => Boolean): MyList[A]
main
@main
def main(): Unit =
val myList: MyList[Int] = MyList(1,2,3)
myList.foreach(x => println(x + 2))
println(myList.map(x => x * 2))
println(myList.filter(x => x < 2))
// 3
// 4
// 5
// MyList(2, 4, 6)
// MyList(1)