list 从 Scala 列表中获取头项和尾项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14804159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:57:08  来源:igfitidea点击:

Get head item and tail items from scala list

listscalasplit

提问by Falmarri

Is there a method in scala to get the (single) head element of a List or Seq and the (collection) tail of the list? I know there's

scala 中是否有一种方法可以获取 List 或 Seq 的(单个)头部元素以及列表的(集合)尾部?我知道有

def splitAt(n: Int): (List[A], List[A])

and I can easily grab the single item from the first list of the tuple. But is there any built in method that is basically this?

我可以轻松地从元组的第一个列表中获取单个项目。但是是否有任何内置方法基本上是这样的?

def splitAtHead: (Option[A], List[A])

Like I said, you can easily chain splitAtto return the right signature, but I figured a built in method might be able to save an intermediate tuple.

就像我说的,您可以轻松地通过链接splitAt返回正确的签名,但我认为内置方法可能能够保存中间元组。

Edit:

编辑:

@om-nom-nom's answer is correct, but this is why I couldn't use his 2nd version.

@om-nom-nom 的回答是正确的,但这就是我不能使用他的第二个版本的原因。

List[S](s1, s2, s3, s4).sortBy { _.f (h) } match {
    case hd :: tail => recurse(tail)
}

回答by om-nom-nom

You can use pattern matching:

您可以使用模式匹配:

val hd::tail = List(1,2,3,4,5)
//hd: Int = 1
//tail: List[Int] = List(2, 3, 4, 5) 

Or just .head/.tail methods:

或者只是 .head/.tail 方法:

val hd = foo.head
// hd: Int = 1
val hdOpt = foo.headOption
// hd: Option[Int] = Some(1)
val tl = foo.tail
// tl: List[Int] = List(2, 3, 4)

回答by Saurav Sahu

The tailmethod returns a collectionconsisting of all elements except the first one (which is basically the head).

tail方法返回一个由除第一个元素(基本上是)以外的所有元素组成的集合head

+------------------+------------------------+-------------------------------+
|      Input       |          head          |             tail              |
+------------------+------------------------+-------------------------------+
| List()           | NoSuchElementException | UnsupportedOperationException |
| List(1)          | 1                      | List()                        |
| List(1, 2, 3, 4) | 1                      | List(2, 3, 4)                 |
| ""               | NoSuchElementException | UnsupportedOperationException |
| "A"              | 'A' (char)             | ""                            |
| "Hello"          | 'H'                    | "ello"                        |
+------------------+------------------------+-------------------------------+

Note that the two methods apply to Stringtype as well.

请注意,这两种方法也适用于String类型。

Answering @Leandro question: Yes we can do that, as shown below:

回答@Leandro 问题:是的,我们可以这样做,如下所示:

scala> var a::b::c = List("123", "foo", 2020, "bar")
a: Any = 123
b: Any = foo
c: List[Any] = List(2020, bar)

scala> var a::b::c = List("123", "foo", "bar")
a: String = 123
b: String = foo
c: List[String] = List(bar)

scala> var a::b::c = List("123", "foo")
a: String = 123
b: String = foo
c: List[String] = List()