Scala take, takeWhile

scala> nums.take(1)
res0: List[Int] = List(1)

scala> nums.take(2)
res1: List[Int] = List(1, 2)

scala> names.take(1)
res2: List[String] = List(joel)

scala> names.take(2)
res3: List[String] = List(joel, ed)
scala> nums.takeWhile(_ < 5)
res4: List[Int] = List(1, 2, 3, 4)

scala> names.takeWhile(_.length < 5)
res5: List[String] = List(joel, ed)

Give you a nice way of taking the elements out of a list that you want to create a new list.

Scala take, takeWhile — Structure map

Clickable & Draggable!

Scala take, takeWhile — Related pages: