从scala.collection.immutable.Iterable [String]中删除第一个和最后一个元素

时间:2014-07-30 05:28:23

标签: scala collections scala-collections

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

osscala.collection.immutable.Iterable[String],当我在控制台中打印

os map println

控制台

sedet impntc
sun
job
03AHJ_VutoHGVhGL70

我想从中删除第一个和最后一个元素。

2 个答案:

答案 0 :(得分:42)

使用drop从正面删除,dropRight从结尾删除。

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

示例:

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

输出:

two
three

答案 1 :(得分:5)

另一种方法是使用slice

val os: Iterable[String] = Iterable("a","b","c","d")
val result = os.slice(1, os.size - 1) // Iterable("b","c")