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有多困難。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章