Table of Contents
|
+++block assignment
This feature can be useful if the initialization of a val takes more than one step.
For example,
val distance = { val dx = x - x0; val dy = y - y0; sqrt(dx * dx + dy * dy) }
In Scala, assignments have no value
{ r = r * n; n -= 1 }
has a Unit value.
functions
You must specify the types of all parameters. However, as long as the function is not recursive, you need not specify the return type.
function literal
function literals and values is that function literals exist in the source code, whereas function values exist as objects at runtime.
(x: Int) => x + 1
The => designates that this function converts the thing on the left (any integer x) to the thing on the right (x + 1).
Function values are objects, so you can store them in variables if you like. They are functions, too, so you can invoke them using the usual parentheses
function-call notation. Here is an example of both activities:
scala> var increase = (x: Int) => x + 1
increase: (Int) => Int = <function1>
scala> increase(10)
res0: Int = 11
variable arguments
def sum(args: Int*) = {
var result = 0
for (arg <- args) result += arg
result
}
val s = sum(1 to 5: _*)
def sum(args: Int*) : Int = {
if (args.length == 0) 0
else args.head + recursiveSum(args.tail : _*)
}
Here, the head of a sequence is its initial element, and tail is a sequence of all
other elements. That’s again a Seq, and we have to use : _* to convert it to an
argument sequence.
array
val result = for (elem <- a if a % 2 == 0) yield 2 * elem
lazy variables
val words = scala.io.Source.fromFile("/usr/share/dict/words").mkString
// Evaluated as soon as words is defined
lazy val words = scala.io.Source.fromFile("/usr/share/dict/words").mkString
// Evaluated the first time words is used
def words = scala.io.Source.fromFile("/usr/share/dict/words").mkString
// Evaluated every time words is used
class
functions:
class Counter {
private var value = 0 // You must initialize the field
def increment() { value += 1 } // Methods are public by default
def current() = value
}
In Scala, a class is not declared as public. A Scala source file can contain multiple classes, and all of them have public visibility.
To use this class, you construct objects and invoke methods in the usual way:
val myCounter = new Counter // Or new Counter()
You can call a parameterless method (such as current) with or without parentheses:
myCounter.current // OK
myCounter.current() // Also OK
Which form should you use? It is considered good style to use () for a mutator method (a method that changes the object state), and to drop the () for an
accessor method (a method that does not change the object state). That’s what we did in our example:
myCounter.increment() // Use () with mutator
println(myCounter.current) // Don’t use () with accessor
You can enforce this style by declaring current without ():
class Counter {
...
def current = value // No () in definition
}
Now the class user must use myCounter.current, without parentheses
properties
class Person() {
var age = 0
}
you can access it like
println(fred.age)
fred.age = 21
in fact, in jvm the property is generated as private member age, and there is
two functions :
private int age;
public int age();
public void age_$eq(int);
println(fred.age) // calls the method fred.age()
fred.age = 21 // class the method fred.age_=(21)
Partial Functions
http://a-kovar.com/blog/2011/08/20/partial-functions-partially-applied-functions-and-currying/
val f:PartialFunction[Double, Double] = {
case x if x!=0 => 1/x
}
According to section 8.5 of the scala language specification This is called a “pattern matching anonymous function” and can be expanded out into either a function or a partial function
Post preview:
Close preview