remove first and last Element from scala.collection.immutable.Iterable[String]

Govind Singh picture Govind Singh · Jul 30, 2014 · Viewed 50.7k times · Source

I am trying to convert my way of getting values from Form, but stuck some where

val os= for {
  m <- request.body.asFormUrlEncoded
  v <- m._2
} yield v

os is scala.collection.immutable.Iterable[String] and when i print it in console

os map println

console

sedet impntc
sun
job
03AHJ_VutoHGVhGL70

i want to remove the first and last element from it.

Answer

Chris Martin picture Chris Martin · Jul 30, 2014

Use drop to remove from the front and dropRight to remove from the end.

def removeFirstAndLast[A](xs: Iterable[A]) = xs.drop(1).dropRight(1)

Example:

removeFirstAndLast(List("one", "two", "three", "four")) map println

Output:

two
three