Scala Cheat Sheet V.0.1 Page 2

ADVERTISEMENT

usage:
except operators ending in colon ':' are treated as
SELECTION
object GraphTest extends Application {
right-associative.
The else must be present and must result in the
val g: Graph = new Graph
An example is the list-consing operator “::”. where,
same kind of value that the if block does
val n1: g.Node = g.newNode
val filename =
x :: y :: zs is interpreted as x :: (y ::
val n2: g.Node = g.newNode
if (options.contains("configFile"))
n1.connectTo(n2)
// legal
zs).
options.get("configFile")
val h: Graph = new Graph
eg.
else
val n3: h.Node = h.newNode
def + (other: Complex) : Complex = {
"default.properties"
n1.connectTo(n3)
// illegal!
//....
}
}
ITERATION
Inner classes are bound to the outer object, so a
Prefer recursion over looping.
node type is prefixed with its outer instance and
Infix Operator:
can't mix instances.
Any single parameter method can be used :
while loop: similar to Java
System exit 0
CASE CLASSES
Thread sleep 10
for loop:
See
for info.
// to is a method in Int that produces a Range
unary operators - prefix the operator name with
object
"unary_"
METHODS/FUNCTIONS
for (i <- 1 to 10; i % 2 == 0) // the left-
def unary_~ : Rational = new Rational(denom,
Methods are Functional Values and Functions are
arrow means "assignment" in Scala
numer)
System.out.println("Counting " + i)
Objects
i <- 1 to 10 is equivalent to:
form: def name(pName: PType1, pName2:
The Scala compiler will try to infer some meaning
for (i <- 1.to(10))
PType2...) : RetType
out of the "operators" that have some
i % 2 == 0 is a filter, optional
use override to override a method
predetermined meaning, such as the += operator.
override def toString() = "" + re + (if (im <
for (val arg <- args)
0) "" else "+") + im + "i"
ARRAYS
Can override for different return type.
maps to args foreach (arg => ...)
arrays are classes
“=>” separates the function's argument list from its
Array[T]
body
More to come...
access as function:
def re = real // method without arguments
a(i)
Anonymous:
parameterize with a type
(function params) | rt. arrow | function body
val hellos = new Array[String](3)
REFERENCES
(x : int, y : int) => x + y
The Busy Developers' Guide to Scala series:
MAIN
“Don't Get Thrown for a Loop”, IBM
OPERATORS
def main(args: Array[String])
developerWorks
All operators are functions on a class.
return type is Unit
“Class action”, IBM developerWorks
Have fixed precedences and associativities:
“Functional programming for the object
(all letters)
ANNOTATIONS
oriented”, IBM developerWorks
|
See
^
Scala Reference Manuals:
&
ASSIGNMENT
“An Overview of the Scala Programming
< >
=
Language” (2. Edition, 20 pages), scala-
= !
protected var x = 0
:
<-
A Brief Scala Tutorial, scala-
+ -
val x <- xs is a generator which produces a
“A Tour of Scala”, scala-
/ %
sequence of values
*
"Scala for Java programmers", A. Sundararajan's
(all other special characters)
Weblog,
Operators are usually left-associative, i.e. x + y + z
is interpreted as (x + y) + z,
"First Steps to Scala",

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 2