Is Scala Only for Computer Scientists?

Is Scala Only for Computer Scientists?

Scala语言是为了计算机科学家设计的吗?

by Bruce Eckel

布鲁斯 埃克尔 著

January 16, 2012

2012年1月16日

Summary
I'm not talking about the early adopters writing obscure code here -- that can probably be solved with a suitable style guide. I just debugged my way through an example that should have been trivial but I only figured out because:
摘要:
我不是在这里说早期的采用者编写晦涩难懂的代码——这可能可以通过一个合适的样式指南来解决。我刚刚调试了一个本应该很简单的示例,但我发现是因为:

ADVERTISEMENT

  1. I have experience struggling through these kinds of things and
  2. I know enough about the subject that I can understand why they did it that way.
  3. 我有过在这些事情中挣扎的经历
  4. 我对这个问题知道得够多了,我能理解他们为什么那样做。

But my concern is that this should be an example that a beginner could understand, and they can't. There's too much depth exposed.

但我担心的是,这应该是一个初学者能够理解的例子,而他们却不能理解。

Here's the example, which is written as a script:

下面是作为脚本编写的示例:

import scala.io.Source._

case class Registrant(line: String) {
  val data = line.split(",")
  val first = data(0)
  val last = data(1)
  val email = data(2)
  val payment = data(3).toDouble
}

val data = """Bob,Dobbs,[email protected],25.00
Rocket J.,Squirrel,[email protected],0.00
Bullwinkle,Moose,[email protected],0.25
Vim,Wibner,[email protected],25.00"""

val lines = fromString(data).getLines
//val lines = fromString(data).getLines.toIndexedSeq
val registrants = lines.map(Registrant)
registrants.foreach(println)
registrants.foreach(println)

The class Registrant takes a String as its constructor argument, and splits it up to produce the various data items stored within that object. Thus you can open a CSV (comma-separated value file, as is produced by most spreadsheets) and parse it into a collection of Registrant objects. You would ordinarily do this by reading in a file using fromFile instead of fromString, which is how I started before seeing weird behavior.

类注册器将字符串作为其构造函数参数,并将其拆分以生成存储在该对象中的各种数据项。因此,您可以打开一个CSV(逗号分隔值文件,由大多数电子表格生成)并将其解析为注册者对象的集合。你通常会用fromFile而不是fromString读入一个文件,这就是我在看到怪异行为之前的开始。

The "strange" behavior is this: as written, the program will only list the registrants once, instead of twice as requested. Indeed, you can't do anything else to your supposed collection of Registrant objects once they've been printed the first time.

“奇怪”的行为是这样的:在编写时,程序只列出注册者一次,而不是按请求列出两次。事实上,一旦注册对象的集合第一次被打印出来,您就不能对它们执行任何其他操作。

Give up? The answer is that registrants is not a collection of any kind. Because getLines returns an iterator (which is the logical thing to do), any functional operation you perform on that iterator also produces an iterator, and you can only use an iterator to pass through your data once. This also makes sense ... after you understand the depth of what's going on, and realize that having "iterators all the way down" is good computer science.

放弃?答案是注册者不是任何类型的集合。因为getLines返回一个迭代器(这是要做的逻辑工作),所以对该迭代器执行的任何函数操作都会生成一个迭代器,并且只能使用迭代器传递一次数据。这也有道理。。。当你了解了正在发生的事情的深度,并意识到拥有“一路向下的迭代器”是很好的计算机科学。

But no posts I looked at that discussed reading files mentioned this, because I suspect the posters (A) didn't know and (B) didn't expect it to work that way, so assumed (logically) that things would work without doing anything else.

但我没有看到讨论阅读文件的帖子提到这一点,因为我怀疑海报(A)不知道,(B)不希望它那样工作,所以假设(逻辑上)事情不会做任何其他事情。

Here's the trick I discovered, although there certainly could be other, better ways to solve it. You have to know that you're getting back an iterator, and explicitly convert it to a regular sequence by calling toIndexedSeq as seen in the commented-out line.

这是我发现的诀窍,尽管肯定还有其他更好的解决方法。您必须知道您正在返回一个迭代器,并通过调用toIndexedSeq显式地将其转换为一个常规序列,如注释行中所示。

This means that, to do something simple and useful that a beginner might find motivating -- like manipulating spreadsheet data from a file -- you'll probably have to explain to your beginner the difference between an iterator and a collection and why an iterator only passes through once, and that you must convert to something called an IndexedSeq. You can choose to wave your hands over the issue but I find that if you throw things at people without explaining them it tends to be confusing.

这意味着,要做一些简单而有用的事情,初学者可能会发现这些事情的动机,比如处理文件中的电子表格数据,您可能需要向初学者解释迭代器和集合之间的区别,以及迭代器为什么只通过一次,你必须转换成IndexedSeq。你可以选择在这个问题上挥手致意,但我发现,如果你不向别人解释就向他们扔东西,往往会让人感到困惑。

You can certainly argue that this is nice and consistent from a computer science standpoint and that the whole language maintains this consistency from top to bottom which makes it quite powerful. And that's great, but it means that before you can start doing useful things you need the kind of breadth and depth of knowledge that only a computer scientist has, and that makes Scala a harder sell as a first programming language (even though many aspects of Scala are significantly easier to grasp than Java or C++).

从计算机科学的角度来看,你可以肯定地说这是一种很好的一致性,而且整个语言从上到下都保持这种一致性,这使得它非常强大。这很好,但这意味着,在你开始做有用的事情之前,你需要的是计算机科学家所拥有的广度和深度知识,这使得Scala作为第一编程语言更难销售(即使Scala的许多方面都比Java或C++更容易掌握)。

For a much more in-depth analysis of Scala complexity by someone with greater knowledge of Scala, see this well-written article. Please note that, just like the author of that article, I'm not saying that Scala is "bad" or "wrong" or things along those lines. I like Scala and think it's a powerful language that will allow us to do things that other languages won't. But in a previous article I suggested that Scala might be a good beginner's language, and "sharp edges" like this that are exposed in what would otherwise be beginning concepts make me wonder if that's true, or if it should actually be relegated to a second or even third language, after the learner has gone through the curve with one or two other languages. So the question is not whether I can figure out this puzzle, or whether it's obvious to you -- since you are probably an experienced programmer -- but rather how much more difficult it might be to teach Scala to an inexperienced programmer.

为了更深入地分析Scala复杂性,了解Scala的知识,请参阅这篇文章。请注意,就像那篇文章的作者一样,我并不是说Scala是“坏的”或“错误的”或是类似的东西。我喜欢Scala,认为它是一种强大的语言,可以让我们做其他语言做不到的事情。但在前一篇文章中,我建议Scala可能是一种很好的初学者语言,而像这样的“锐利的边缘”暴露在那些原本是初级概念的地方,让我怀疑这是否正确,或者,在学习者使用一种或两种其他语言完成曲线后,是否真的应该将其归入第二种甚至第三种语言。所以问题不在于我是否能解决这个难题,也不在于它对你来说是否显而易见——因为你可能是一个有经验的程序员——而是告诉一个没有经验的程序员Scala有多困难。

发布了88 篇原创文章 · 获赞 33 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/ccmedu/article/details/103825464