The Artima Developer Community
Sponsored Link

Computing Thoughts
Is Scala Only for Computer Scientists?
by Bruce Eckel
January 16, 2012
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.

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,bob@dobbs.com,25.00
Rocket J.,Squirrel,rocky@frostbite.com,0.00
Bullwinkle,Moose,bull@frostbite.com,0.25
Vim,Wibner,vim32@goomail.com,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.

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.

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.

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.

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.

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++).

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.

Talk Back!

Have an opinion? Readers have already posted 17 comments about this weblog entry. Why not add yours?

RSS Feed

If you'd like to be notified whenever Bruce Eckel adds a new entry to his weblog, subscribe to his RSS feed.

About the Blogger

Bruce Eckel (www.BruceEckel.com) provides development assistance in Python with user interfaces in Flex. He is the author of Thinking in Java (Prentice-Hall, 1998, 2nd Edition, 2000, 3rd Edition, 2003, 4th Edition, 2005), the Hands-On Java Seminar CD ROM (available on the Web site), Thinking in C++ (PH 1995; 2nd edition 2000, Volume 2 with Chuck Allison, 2003), C++ Inside & Out (Osborne/McGraw-Hill 1993), among others. He's given hundreds of presentations throughout the world, published over 150 articles in numerous magazines, was a founding member of the ANSI/ISO C++ committee and speaks regularly at conferences.

This weblog entry is Copyright © 2012 Bruce Eckel. All rights reserved.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use