Tuesday, September 5, 2017
Scala book chapter 3 review notes stream of conscienceness Part 2
Scala book chapter 3 review notes stream of conscienceness Part 2
Scala 2 Book Chapter 3 review.
More of my random thoughts as I go through the Scala book.
Going through Chapter 3, ....
val numNames = Array("zero", "one", "two", "three")
numNames(0) = "0"
for (i <-0 to numNames.length-1) {
val name = numNames(i)
println (s"name i $i = $name")
}For some reason I did not think that Scala had a for loop as most of the example I see use a while loop. But it does.
Java version of this:
final String[] numNames = {"zero", "one", "two", "three"};
numNames[0] = "0";
for (int i = 0; i < numNames.length; i++) {
out.printf ("name i %d = %s ", i, numNames[i]);
}Other than the Java not supporitng implicit typing which it does sort of on the right hand side of the assingment, which is oddly inconsistent, there is not much difference LOC wise between these two. Scala one if you iterating of millions and million in a tight loop which was iterating of millions and million which was getting called in a microservice a million times a second, would cause a lot more GC given the even Integers are objects mantra (I would imagine but a good hotspot and Scala compiler could optmize most of that away.. do they? Based on perf of JSON parsers written in purse Scala, I have doubts, but I am no Scala expert.)
Scala doesnt technically have operator overloading, because it doesnt actually have operators - Odersky, Martin; Spoon, Lex; Venners, Bill (2010-12-13). Programming in Scala: A Comprehensive Step-by-Step Guide (Kindle Locations 923-924). Artima Press. Kindle Edition.
Which is technially what Python and C++ have but they call it operator overloading and so should you. Prior art. But you know whatever.
The book brags about the consitency of always using the object model.
Lets cover that a bit.
Here are two different ways to access a Map in scala.
x += (1 -> "foo")
x(1) = "foo2"Here are five different ways to iterate through the list of numNames.
val numNames = Array("zero", "one", "two", "three")
numNames(0) = "0"
for (i <-0 to numNames.length-1) {
val name = numNames(i)
println (s"name i $i = $name")
}
for (i <-0.to(numNames.length-1)) {
val name = numNames(i)
println (s"name i $i = $name")
}
var i = 0
while (i < numNames.length) {
val name = numNames(i)
println (s"name i $i = $name")
i+=1
}
i = 0
numNames.foreach(name => {
println (s"name i $i = $name")
i+=1
})
i = 0
for (name <- numNames) {
println (s"name i $i = $name")
i+=1
}
Note the only difference between the first and second way is how I invoke the to method. One way is the operator way. One way is the normal Java . method invoke way.
I find Scala about as consistent at doing things as Perl so far, but I am new. I could see two developers writing the same program completely different.
I do find a lot of value in Scala. Its consistent Object method invocation is not one of them (so far).
List and Cons
::: :: Yuck!
Appending lists. Just as simple as this:
In the expression "1 :: twoThree", :: is a method of its right operand, the list, twoThree. ...If a method is used in operator notation, such as a b, the method is invoked on the left operand, as in a.(b)�unless the method name ends in a colon. If the method name ends in a colon, the method is invoked on the right operand. --Odersky, Martin; Spoon, Lex; Venners, Bill (2010-12-13). Programming in Scala: A Comprehensive Step-by-Step Guide (Kindle Locations 985-997). Artima Press. Kindle Edition.
I guess I willget used to this. Right now I am not liking it much. I prefer Pythons view of consistency.
I thought I understood what they said.
I tried it first in Java.
final List<String> aList = asList("1", "2", "3");
final List<String> bList = asList("4", "5", "6");
final List<String> both = new ArrayList<>();
both.addAll(aList);
both.addAll(bList);
out.printf("Both %s ", both);I got this
Both [1, 2, 3, 4, 5, 6]
Then I tried the same thing in Scala.
val aList = List("1", "2", "3")
val bList = List("4", "5", "6")
val both = aList :: bList
println(s"Both $both")I got this
Both List(List(1, 2, 3), 4, 5, 6)
Ok.. that was not what I wanted. Let me try the extra colon :::.
val aList = List("1", "2", "3")
val download file now
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.