Saturday, October 29, 2016

Programming in Scala 7/8/9/10

1.

scala> val someNumbers = List(-11,2,-12,8,9)
scala> someNumbers.filter( x=>x<8 )
scala> someNumbers.filter(_>0)
scala> val f= (_: Int)+(_: Int)
scala> someNumbers.foreach(println _)
 equals
 someNumbers.foreach(x => println(x))
2. closure

 def makeIncreaser(more: Int) = (x: Int) => x + more
3.
 scala> def twice(op: Double => Double, x: Double) = op(op(x))
 scala> twice(_ + 1, 5)
4.
In Scala it is forbidden to define a field and method with the same name in the same class, whereas this is
allowed in Java.
Generally, Scala has just two namespaces for definitions in place of Java's four. Java's four namespaces
are fields, methods, types, and packages. By contrast, Scala's two namespaces are:
• values (fields, methods, packages, and singleton objects)
• types (class and trait names)

Tuesday, October 25, 2016

Programming in Scala Note 4/5/6

1.

The way you make members public in Scala is by not explicitly specifying any access modifier.
Public is Scala's default access level.
2.
one way in which Scala is more object-oriented than Java is that classes in
Scala cannot have static members. Instead, Scala has singleton objects. A singleton object definition
looks like a class definition, except instead of the keyword class you use the keyword object.
A singleton object is more than a holder of static methods, however. It is a first-class object.
One difference between classes and singleton objects is that singleton objects cannot take parameters,
whereas classes can. Because you can't instantiate a singleton object with the newkeyword, you have
no way to pass parameters to it.
3.
in Scala, you
can name .scala files anything you want, no matter what Scala classes or code you put in them.
4. ==First check the left side for null. If it is not null,

call the equals method.
scala> 1==1.0

res8: Boolean = true
5. 
In Java, classes have
constructors, which can take parameters; whereas in Scala, classes can take parameters directly. The

Scala notation is more concise—class parameters can be used directly in the body of the class
6.
 class Rational(n: Int, d: Int) {
require(d != 0)
override def toString = n + "/" + d
}
7. 
In Scala, every auxiliary constructor must invoke another constructor of the same class as its first
action. In a Scala class, only the primary
constructor can invoke a superclass constructor.
8.
 scala> implicit def intToRational(x: Int) = new Rational(x)

Programming in Scala Note Chapter 1/2/3

Chapter 1/2/3
1.

def factorial(x: BigInt): BigInt =
    if (x == 0) 1 else x * factorial(x - 1)
2.
A val is similar to a final variable in Java. Once
initialized, a val can never be reassigned. A var, by contrast, is similar to a non-final variable in Java. 
3.
Sometimes the Scala compiler will require you to specify the result type of a function. If the function
is recursive,[7] for example, you must explicitly specify the function's result type.
If a function consists of just one statement, you can optionally leave off the curly braces.
scala> def max(x: Int, y: Int) = if (x > y) x else y
4.
scala> def greet() = println("Hello, world!")
greet: ()Unit
A result type of Unit indicates the function returns no interesting value.
5.
Note that Java's + +i and i++ don't work in Scala. To increment in Scala, you need to say either i = i + 1 or i += 1.
6.
args.foreach(arg => println(arg))
or   
args.foreach((arg: String) => println(arg))
or
args.foreach(println)
or
for(arg <- args) println(arg)
7.
If the method name ends in a colon, the
method is invoked on the right operand.
8.
The time it takes to append to a list grows linearly with the size of the
list, whereas prepending with :: takes constant time. If you want to build a list efficiently by appending
elements, you can prepend them and when you're done call reverse. Or you can use a ListBuffer, a
mutable list that does offer an append operation, and when you're done call toList.
9.
Once you have a tuple instantiated, you can access its elements individually with a dot,
underscore, and the one-based index of the element.
val pair = (99, "Luftballons")
println(pair._1)
10.
This -> method, which you can invoke on any
object in a Scala program, returns a two-element tuple containing the key and value.
import scala.collection.mutable
val treasureMap = mutable.Map[Int, String]()
treasureMap += (1 -> "Go to island.")
11.
if code contains any vars, it is probably in an imperative style. If the code contains no vars at all—i.e., it
contains only vals—it is probably in a functional style.