02 getting started with scala
TRANSCRIPT
-
8/6/2019 02 Getting Started with Scala
1/25
5-Jun-11
Getting Started with Scala
-
8/6/2019 02 Getting Started with Scala
2/25
Hello World
/** Everybodys first program */object HelloWorld {
def main(args: Array[String]) {println("Hello, World!")
}
} Save on a file named HelloWorld.scala
Compile with scalac HelloWorld.scala (or from your IDE)
Run with scala HelloWorld (or from your IDE)
Generate documentation with scaladoc HelloWorld.scala
The above code creates a single object
Everything in the object is like Javas static
Scala doesnt have static
Scala does have classes; well get to those later
2
-
8/6/2019 02 Getting Started with Scala
3/25
Comments
// and /*...*/ comments are as in Java
/** Scaladoc comments are similar to Javadoc
* comments. As in Javadoc, the first sentence* is a summary sentence, and additional* sentences give more detail. However, the* formatting convention is slightly different,
* and wiki markup is used in preference to* HTML markup. Most of the same flags* (@author, etc.) are used. */
def doNothing = ()
-
8/6/2019 02 Getting Started with Scala
4/25
Types
The type hierarchy is a lattice, not a tree
That is, it has a bottom as well as a top
Any AnyVal
Boolean, Char, Byte, Short, Int, Long, Float, Double Scala has no primitivesthese are objects
Unit (has only a single value, ()
Unit is returned by functions that don't return anything (e.g. println)
AnyRef (corresponds to Object in Java)
All Java reference types, for example, String ScalaObject
All Scala reference types, including Array and List
Null (bottom of all AnyRef objects)
Nothing (bottom ofAny)
4
-
8/6/2019 02 Getting Started with Scala
5/25
Declaring variables and values
The syntax is
var name: type = value // declares a variableval name: type = value // declares a value
Values are immutable: they cant be changed
The : type can almost always be left offthe type is inferred from the value
The = value must (almost) always be supplied
This also works: var x: Double = 5
Rules for alphanumeric names are just like in Java
But there are other kinds of names
Scope rules are just like in Java
Capitalization conventions are just like in Java
Arithmetic, comparison, and logical operators are just like in Java
Indentation is 2 spaces, not 4, but is otherwise the same as in Java
5
-
8/6/2019 02 Getting Started with Scala
6/25
Statements
Scalas statements should really be called expressions, because almost
every statement has a value
For example, the value ofa = 5 is 5
The value of many statements, for example the while loop, is () () is a value of type Unit
() is the only value of type Unit
()basically means Nothing to see here. Move along.
The value of a block, {}, is the last value computed in the block
A statement is ended by the end of the line (not with a semicolon) unless it is
obviously incomplete For example, x = 3 * (2 * y + is obviously incomplete
Because Scala lets you leave out a lot of unnecessary punctuation, sometimes a line
that you thinkis complete really isnt complete (or vice versa)
You can end statements with semicolons, but thats not good Scala practice
6
-
8/6/2019 02 Getting Started with Scala
7/25
Familiar statement types
These are the same as in Java (but have a value):
variable = expression // also +=, *=, etc. The value of the statement is the value of the variable
while (condition) { statements } The value of the statement is ()
do { statements } while (condition) The value is ()
if (condition) { statements } The value is the value of the last statement executed
if (condition) { statements } else { statements } Older descriptions of Scala say that the else is required. It isnt--if
omitted and the condition is false, the value of the if statement is ()
7
-
8/6/2019 02 Getting Started with Scala
8/25
The for comprehension
Scalas for is much more powerful than Javas for Consequently, it is used much more often than the other kinds of loops
We will just cover some simple cases here
for (i
-
8/6/2019 02 Getting Started with Scala
9/25
Arrays
Arrays in Scala areparameterized types Array[String] is an Array of Strings, where String is a type parameter
In Java we would call this a generic type
When no initial values are given, new is required, along with an
explicit type: val ary = new Array[Int](5)
When initial values are given, new is not allowed: val ary2 = Array(3, 1, 4, 1, 6)
Arrays in Scala are just another kind of object; they have no
special syntax
Scalas Lists are more useful, and used more often, than Arrays val list1 = List(3, 1, 4, 1, 6)
val list2 = List[Int]() // An empty list must have an explicit type
-
8/6/2019 02 Getting Started with Scala
10/25
Simple List operations
By default, Lists, like Strings, are immutable Operations on an immutable List return a new List
Basic operations:
list.head (orlisthead) returns the first element in the list
list.tail (orlisttail) returns a list with the first element removed list(i) returns the ith element (starting from 0) of the list
list(i) = value is illegal (immutable, remember?)
value :: listreturns a list with value appended to the front
list1 ::: list2 appends (concatenates) the two lists
list.contains(value) (orlistcontains value) tests whethervalue is in list An operation on a List may return a List of a different type
scala> "abc" :: List(1, 2, 3)res22: List[Any] = List(abc, 1, 2, 3)
There are over 150 built-in operations on Lists--use the API!
-
8/6/2019 02 Getting Started with Scala
11/25
Tuples
Scala has tuples, up to size 22 (why 22? I have no idea.)
scala> val t = Tuple3(3, "abc", 5.5)t: (Int, java.lang.String, Double) = (3,abc,5.5)
scala> val tt = (3, "abc", 5.5)
tt: (Int, java.lang.String, Double) = (3,abc,5.5) Tuples are referencedstarting from 1, using _1, _2, ...
scala> t._1res28: Int = 3
t _1 also works (the dot is optional) Tuples, like Lists, are immutable
Tuples are a great way to return more than one valuefromamethod!
-
8/6/2019 02 Getting Started with Scala
12/25
An aside...abbreviations
Scala lets you omit a lot of unnecessary punctuation
For example,
if (name startsWith "Dr.") { ... }is more readable (and easier to type) than
if (name.startsWith("Dr.")) { ... }
Readability matters!
Therefore, you should experiment with leaving out
punctuation anywhere you think it might be okay However,
If you get mysterious syntax errors, try putting the punctuation
back in, both on this line and on the previous line
-
8/6/2019 02 Getting Started with Scala
13/25
Maps
scala> val m = Map("apple" -> "red", "banana" -> "yellow")m: scala.collection.immutable.Map[java.lang.String,java.lang.String]= Map((apple,red), (banana,yellow))
Notice that a Map is really just a list ofTuples
The -> is provided as a more readable syntax
scala> m("banana")res2: java.lang.String = yellow
scala> m contains "apple"
res3: Boolean = true
scala> m("cherry")java.util.NoSuchElementException: key not found: cherry
-
8/6/2019 02 Getting Started with Scala
14/25
Simple function definitions
def isEven(n: Int) = {val m = n % 2m == 0
} The result is the last value (in this case, a Boolean)
def isEven(n: Int) = n % 2 == 0 The result is just a single expression, so no braces are needed
def countTo(n: Int) {for (i
-
8/6/2019 02 Getting Started with Scala
15/25
Functions are first-class objects
Functions are values (like integers, etc.) and can be assigned tovariables, passed to and returned from functions, and so on
Wherever you see the => symbol, its a literal function
Example (assigning a literal function to the variable foo):
scala> val foo = (x: Int) => if (x % 2 == 0) x / 2 else 3 * x + 1foo: (Int) => Int =
scala> foo(7)res28: Int = 22
The basic syntax of a function literal is
parameter_list=> function_body In this example, foreach is a function that takes a function as a
parameter:
myList.foreach(i => println(2 * i))
-
8/6/2019 02 Getting Started with Scala
16/25
Functions as parameters
To define a function, you must specify the types of each of its parameters
Therefore, to have a function parameter, you must know how to write its type:
(type1, type2, ..., typeN) => return_type
type => return_type // if only one parameter
Example:
scala> def doTwice(f: Int => Int, n: Int) = f(f(n))doTwice: (f: (Int) => Int,n: Int)Int
scala> def collatz(n: Int) = if (n % 2 == 0) n / 2 else 3 * n + 1collatz: (n: Int)Int
scala> doTwice(collatz, 7)res2: Int = 11
scala> doTwice(a => 101 * a, 3)res4: Int = 30603
-
8/6/2019 02 Getting Started with Scala
17/25
Higher-order methods on Lists
map applies a one-parameter function to every element of a List, returning anew List
scala> def double(n: Int) = 2 * ndouble: (n: Int)Int
scala> val ll = List(2, 3, 5, 7, 11)ll: List[Int] = List(2, 3, 5, 7, 11)
scala> ll map doubleres5: List[Int] = List(4, 6, 10, 14, 22)
scala> ll map (n => 3 * n)res6: List[Int] = List(6, 9, 15, 21, 33)
scala> ll map (n => n > 5)res8: List[Boolean] = List(false, false, false, true, true)
filter applies a one-parameter test to every element of a List, returning a Listof those elements that pass the test
scala> ll filter(n => n < 5)res10: List[Int] = List(2, 3)
scala> ll filter (_ < 5) // abbreviated function where one parameter is used onceres11: List[Int] = List(2, 3)
-
8/6/2019 02 Getting Started with Scala
18/25
More higher-order methods
def filterNot(p: (A) => Boolean): List[A] Selects all elements of this list which do not satisfy a predicate
def count(p: (A) => Boolean): Int Counts the number of elements in the list which satisfy a predicate
def forall(p: (A) => Boolean): Boolean Tests whether a predicate holds for every element of this list
def exists(p: (A) => Boolean): Boolean Tests whether a predicate holds for at least one of the elements of this list
def find(p: (A) => Boolean): Option[A] Finds the first element of the list satisfying a predicate, if any
def sortWith(lt: (A, A) => Boolean): List[A] Sorts this list according to a comparison function
-
8/6/2019 02 Getting Started with Scala
19/25
-
8/6/2019 02 Getting Started with Scala
20/25
The Option type
Scala has nullbecause it interoperates with Java; itshouldnt be used any other time
Instead, use an Option type, with values Some(value)and None def max(list: List[Int]) = {
if (list.length > 0) {val biggest = (list(0) /: list) { (a, b) => if (a > b) a else b }Some(biggest)
} else {None
} max(myList) match {
case Some(x) => println("The largest number is " + x)case None => println("There are no numbers here!!!")
}
-
8/6/2019 02 Getting Started with Scala
21/25
The require and assert methods
require and assert are methods that throw an exception whentheir argument is false
require is used to document something that must be true in order
for the code to work def sqrt(x: Double) = { require(x >= 0); ... }
require is often used at the beginning of a method
assert is used to document something that you know to be true takeCis700course
assert(languagesIKnow contains "Scala")
assert is often used at the end of a block of code, to say what that code hasaccomplished
-
8/6/2019 02 Getting Started with Scala
22/25
Dealing with exceptions
Scalas exception creation and throwing is like Java:
class RottenEggException extends Exception
throw new RottenEggException
Catching a thrown exception uses pattern matching: try {
makeAnOmlet} catch {
case ex: RottenEggException => println("#$%@")
case ex: Exception => println("What went wrong?")}
-
8/6/2019 02 Getting Started with Scala
23/25
File I/O
object TestIO {def main(args: Array[String]) {println("Testing file I/O")
import java.io._import scala.io.Sourceval file = new File("testio.txt")
val writer = new PrintWriter(file)writer write "first\r\n"writer write "second"writer.close()
val lines = Source.fromFile(file).getLines().toListprintln(lines)
}}
Testing file I/OList(first, second)
Use correct case for file names
(only Windows ignores case)
Use forward slashes, /, in paths,which work on any platform, not
backslashes,\, which work only on
Windows
Windows,Mac, and Linux have
different end-of-line codes (\r\n isWindows), and this causes
problems
-
8/6/2019 02 Getting Started with Scala
24/25
Use the source, Luke
Books and tutorials are good for learning the syntax of Scala, but they arent much help
learning the API
Unfortunately, Scalas API documentation isnt very complete
The good news is, its easy to get to the source code--and in most cases, the source
code is easier to read than you might think
-
8/6/2019 02 Getting Started with Scala
25/25
25
The End