scala.Array 如何是 Seq? [英] How is scala.Array a Seq?

查看:39
本文介绍了scala.Array 如何是 Seq?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名强大的 Java 开发人员,最近我开始尝试在空闲时间学习 Scala.我正在阅读 scala-lang 的 Scala by Example PDF.org 并且很困惑第一个示例中的快速排序是如何工作的.代码如下:

I'm a strong Java developer who has very recently started trying to pick up Scala in my free time. I'm going through the Scala by Example PDF from scala-lang.org and am confused how the Quick Sort in the very first example works. Here is the code:

object QuickSort extends App {

  def sort(input: Array[Int]): Array[Int] = {
    if(input.length <= 1) input
    else
    {
      val pivot = input(input.length / 2)
      Array.concat(
          sort(input filter (pivot >)),
               input filter (pivot ==),
          sort(input filter (pivot <))
      )
    }
  }

  sort(Array(5, 4, 3, 2, 1)) foreach println
}

我的问题不在于语法或任何东西,但我对过滤器函数的来源感到困惑.根据 PDF,它说它来自 Seq[T] 类,并且所有数组都是 Seq[T] 的实例.这一切都很好,而且在阅读 PDF 时我很满意,并且是一个非常快乐的 Scala 新手开发人员.但后来我深入挖掘并开始查看 scaladoc for Array[T] 以及 Array[T] 的源代码,我根本看不到 Array[T] 类是如何扩展或继承 Seq[T] 特性的.我错过了什么?

My question is not with the syntax or anything, but I am confused with where the filter function comes from. According to the PDF, it says that it comes from the Seq[T] class, and that all Arrays are instances of Seq[T]. That is all fine and dandy and while reading the PDF I was satisfied and a very happy newbie Scala developer. But then I dug a little deeper and started looking at the scaladoc for Array[T] and also the source code for Array[T] and I do not see how the Array[T] class extends or inherits the Seq[T] trait at all. What am I missing?

推荐答案

您缺少隐式.有一个少数 问题关于

You are missing implicits. There's a few questions about implicits on Stack Overflow. On the PDF you are reading, see chapter 15, starting on page 113. On Scaladoc, you'll see the relevant implicits on the object scala.Predef -- just look for implicit methods which take an Array as input parameter and return something else.

PS:哎呀,它说Array 是一个Seq!实际上,在 Scala 2.8 之前可能就是这种情况,但从那时起,Array 就是 Java Array,纯粹而简单.

PS: Yikes, it says Array is a Seq! That might have been the case before Scala 2.8, actually, but since then an Array is a Java Array, pure and simple.

这篇关于scala.Array 如何是 Seq?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆