Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 840
Govind Singh Avatar asked Jul 30 '14 05:07

Govind Singh


2 Answers

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 
like image 174
Chris Martin Avatar answered Sep 18 '22 16:09

Chris Martin


Another way is to use slice.

val os: Iterable[String] = Iterable("a","b","c","d") val result = os.slice(1, os.size - 1) // Iterable("b","c") 
like image 27
Kigyo Avatar answered Sep 19 '22 16:09

Kigyo