Monday 26 August 2013

The for Loop “Reads”. Not Sure About Some of the Vector Methods Though…

The claim that Scala doesn’t “force you to jump through hoops [with the syntax]” (Atomic Scala pp.109-110) holds true for the for loop, especially when you “read” it as follows:

// “for i gets each value of the Vector"
for (i <- Vector(1, 2, 3, 4)) {
    println(“the value of I is: “ i)
}

I especially like that it parses in this instance, in the same way a lot of Ruby syntax does.  (Update, 5th September, 2013: As Summary 2, pp. 126-7 of Atomic Scala points out, the Scala “for” focusses on the loop, rather than the counting.  Precisely. And if you want to count? Loop through a range.  Beautifully explicit. You even get the Range’s “to” and “until” methods. Luverly)

But what about “Vector.sorted”?  I follow the argument (Atomic Scala pp.110) that Scala likes to “leave things in place” and instead create new copies of things. But for me, a good method name always is, or starts with a verb: getXxxx, sortXxxx, calculateXxxx.  That’s because methods act on the object they belong to, using the parameters they are passed.

But is this just another thing I’m trying to carry across from the well-worn Java/OO world?  My dictionary tells me (we focussed more on creativity at my school…) “sorted” is an adjective, and my dictionary also tells me that an adjective is

“a word naming an attribute of a noun", such as sweet”

(Oxford Dictionary of English)

Now if we see it in context:

val sortedVal = v4.sorted

And read it, knowing that Scala prefers to leave the original objects alone, and return a copy (and in this case a sorted copy) then perhaps I can warm to this.  I don’t think I’ll have too much of a problem, as long as things are consistent in this rule.  But doesn’t “Vector.sum” already break this rule? (Shouldn’t it be “summed”?)

No comments:

Post a Comment