20110515 scala by_example
-
Upload
linuxfb -
Category
Technology
-
view
847 -
download
1
Transcript of 20110515 scala by_example
Scala via Examples
Hu Zi Ming
201l-05-15
1 / 21Scala via Examples
N
Outline
1 What is Scala
2 GCD Algorithm
3 Grep
4 Advanced Scala
2 / 21Scala via Examples
N
What is Scala
JVM based language
Combine OOP and FP
Scalable on architecture and performance
3 / 21Scala via Examples
N
What is Scala
JVM based language
Combine OOP and FP
Scalable on architecture and performance
3 / 21Scala via Examples
N
What is Scala
JVM based language
Combine OOP and FP
Scalable on architecture and performance
3 / 21Scala via Examples
N
Outline
1 What is Scala
2 GCD Algorithm
3 Grep
4 Advanced Scala
4 / 21Scala via Examples
N
GCD Algorithm
Greaest Common Divisor (gcd.scala)
def gcd (m: Int, n: Int): Int = (m, n) match {case (m, ‘m‘) => mcase (m, n) if n > m => gcd(n, m)case (m, 0) => mcase _ => gcd(n, m % n)
}
println(gcd(20, 30))
5 / 21Scala via Examples
N
Compile and Run it
Method 1:
scala script-name
Method 2 (Need main method in object object):
scalac script-name
scala object-name
6 / 21Scala via Examples
N
Compile and Run it
Method 1:
scala script-name
Method 2 (Need main method in object object):
scalac script-name
scala object-name
6 / 21Scala via Examples
N
Cases for scala and scalac
Running as scala foo.scala
println("Foo")
Running as scalac foo.scala; scala Foo
object Foo {def main(args: Array[String]) = println("Foo")
}
java.lang.NoSuchMethodException: Foo.main is not static
class Foo {def main(args: Array[String]) = println("Foo")
}
7 / 21Scala via Examples
N
Declare a method
Start with def keyword
Then function name
Followed by optional argument list
Then return type follower by a colon
Equal sign and method body
8 / 21Scala via Examples
N
Declare a method
Start with def keyword
Then function name
Followed by optional argument list
Then return type follower by a colon
Equal sign and method body
8 / 21Scala via Examples
N
Declare a method
Start with def keyword
Then function name
Followed by optional argument list
Then return type follower by a colon
Equal sign and method body
8 / 21Scala via Examples
N
Declare a method
Start with def keyword
Then function name
Followed by optional argument list
Then return type follower by a colon
Equal sign and method body
8 / 21Scala via Examples
N
Declare a method
Start with def keyword
Then function name
Followed by optional argument list
Then return type follower by a colon
Equal sign and method body
8 / 21Scala via Examples
N
Outline
1 What is Scala
2 GCD Algorithm
3 Grep
4 Advanced Scala
9 / 21Scala via Examples
N
Grep
Grep (grep.scala)
import scala.io.Source
class Greper(pattern: String, filename: String) {def execute = {
for (line <- Source.fromFile(filename).getLines) {if (line.trim.matches(pattern))
println(line)}
}}
object LearnScala {def main(args: Array[String]) = {
val pattern = args(0)val filename = args(1)
val foo = new Greper(pattern, filename)foo.execute
}}
10 / 21Scala via Examples
N
Singleton
There is no static method in Scala
But singleton object
Singleton object can not be created by new
Singleton object can not accept arguments
Singleton object will be created when first accessed
11 / 21Scala via Examples
N
Singleton
There is no static method in Scala
But singleton object
Singleton object can not be created by new
Singleton object can not accept arguments
Singleton object will be created when first accessed
11 / 21Scala via Examples
N
Singleton
There is no static method in Scala
But singleton object
Singleton object can not be created by new
Singleton object can not accept arguments
Singleton object will be created when first accessed
11 / 21Scala via Examples
N
Singleton
There is no static method in Scala
But singleton object
Singleton object can not be created by new
Singleton object can not accept arguments
Singleton object will be created when first accessed
11 / 21Scala via Examples
N
Command Line Arguments
Cmd line arguments will be passed into main function
The main func should be contained in singleton object
java.lang.NoSuchMethodException: Foo.main is not static
object Foo {def main(args: Array[String]) = args.foreach(println)
}
12 / 21Scala via Examples
N
Immutable VS. Mutable
Variable can be declared in two types
val and var
val means value, immutable
var means variable, mutable
Immutable variable can be changed by reference
Like constant pointer in C/C++
13 / 21Scala via Examples
N
Immutable VS. Mutable
Variable can be declared in two types
val and var
val means value, immutable
var means variable, mutable
Immutable variable can be changed by reference
Like constant pointer in C/C++
13 / 21Scala via Examples
N
Immutable VS. Mutable
Variable can be declared in two types
val and var
val means value, immutable
var means variable, mutable
Immutable variable can be changed by reference
Like constant pointer in C/C++
13 / 21Scala via Examples
N
List and Tuple
Basic containers in scala
List object and its element are immutable
List can only contains elements in same type
Combine two list means create a new one
Element in list can be accessed via [idx]
Tuple object and its element are immutable
Tuple can contains elements in different type
Tuple using 1-based index
Element in Tuple can be accessed via idx
14 / 21Scala via Examples
N
List and Tuple
Basic containers in scala
List object and its element are immutable
List can only contains elements in same type
Combine two list means create a new one
Element in list can be accessed via [idx]
Tuple object and its element are immutable
Tuple can contains elements in different type
Tuple using 1-based index
Element in Tuple can be accessed via idx
14 / 21Scala via Examples
N
List and Tuple
Basic containers in scala
List object and its element are immutable
List can only contains elements in same type
Combine two list means create a new one
Element in list can be accessed via [idx]
Tuple object and its element are immutable
Tuple can contains elements in different type
Tuple using 1-based index
Element in Tuple can be accessed via idx
14 / 21Scala via Examples
N
Object is Everything
Every function is value, and every value is object
Hence everything is object
1+2*3/x means 1.+(2.*(3./(x)))
1::2::3::4::Nil means 1.::(2.::(3.::(4.::(Nil))))
15 / 21Scala via Examples
N
Object is Everything
Every function is value, and every value is object
Hence everything is object
1+2*3/x means 1.+(2.*(3./(x)))
1::2::3::4::Nil means 1.::(2.::(3.::(4.::(Nil))))
15 / 21Scala via Examples
N
Object is Everything
Every function is value, and every value is object
Hence everything is object
1+2*3/x means 1.+(2.*(3./(x)))
1::2::3::4::Nil means 1.::(2.::(3.::(4.::(Nil))))
15 / 21Scala via Examples
N
Outline
1 What is Scala
2 GCD Algorithm
3 Grep
4 Advanced Scala
16 / 21Scala via Examples
N
Operators and Order
Operators can be defined by user
Some syntactic sugar can be implemented by this
Operator ends with ‘:’ means right associative
a * b yields a.*(b)
While a ::: b yields b.:::(a)
17 / 21Scala via Examples
N
Operators and Order
Operators can be defined by user
Some syntactic sugar can be implemented by this
Operator ends with ‘:’ means right associative
a * b yields a.*(b)
While a ::: b yields b.:::(a)
17 / 21Scala via Examples
N
Case Class
Scalas way to allow pattern matching on objects
18 / 21Scala via Examples
N
Curry Example
Plain-old function
def foo(x: Int, y: Int) = x + yfoo(1, 2)
Curried function
def bar(x: Int)(y: Int) = x + ybar(1)(2)
19 / 21Scala via Examples
N
Curry
Curry function bar means two traditional func invocation backto back
First func take Int x as parameter
And return a func value for second func
This func take the Int y as parameter
Func declared in other method
def bar2_first(x: Int) = (y: Int) => x + ydef bar2_second = bar2_first(1)bar2_second(2)
20 / 21Scala via Examples
N
Curry
Curry function bar means two traditional func invocation backto back
First func take Int x as parameter
And return a func value for second func
This func take the Int y as parameter
Func declared in other method
def bar2_first(x: Int) = (y: Int) => x + ydef bar2_second = bar2_first(1)bar2_second(2)
20 / 21Scala via Examples
N
Curry
Curry function bar means two traditional func invocation backto back
First func take Int x as parameter
And return a func value for second func
This func take the Int y as parameter
Func declared in other method
def bar2_first(x: Int) = (y: Int) => x + ydef bar2_second = bar2_first(1)bar2_second(2)
20 / 21Scala via Examples
N
Curry
Curry function bar means two traditional func invocation backto back
First func take Int x as parameter
And return a func value for second func
This func take the Int y as parameter
Func declared in other method
def bar2_first(x: Int) = (y: Int) => x + ydef bar2_second = bar2_first(1)bar2_second(2)
20 / 21Scala via Examples
N
Q AND A
21 / 21Scala via Examples
N