Wednesday, January 09, 2013

Scala Learnings #3

From reading Scala for the Impatient chapter 1, I have learnt the following:

In the Scala REPL, code completion is available using <Tab>. Also, the result of an expression can be used in further expressions.

scala> "Homer"
res7: java.lang.String = Homer

scala> res7 * 2
res8: String = HomerHomer

Declaration of multiple values with the same type is possible.

val maxFoo, maxBar : Int = 100

1 to 10 is the infix notation for 1.to(10) which defines a range of 1 to 10.

val range = 1 to 10

The convenience APIs for java.lang.String can be found on the StringOps class. The convenience APIs for java.lang.Int, java.lang.Long, etc. can be found on RichInt, RichLong, etc.

Methods, not casts, are used to convert between numeric types.

99.44.toInt ; "hello".asInstanceOf[CharSequence]

Rule of thumb is that parameterless methods that do not modify the object have no parenthesis.

List(1,2,3,1,2,3) distinct

Creating a new BigInt instance without the new keyword is possible because of the BigInt.apply(String) method on the companion object.

BigInt("12345")

The mathematical functions are in the package scala.math, not in any class.

pow(2,8)

Scala allows for a very compact notation when calling higher-order functions.

"HeLlO".count(_.isUpper)

No comments: