Scala via Examples     Hu Zi Ming hzmangel@gmail.com     201l-05-15                      Scala via Examples               ...
Outline1   What is Scala2   GCD Algorithm3   Grep4   Advanced Scala                               Scala via Examples      ...
What is ScalaJVM based language                                 Scala via Examples                                        ...
What is ScalaJVM based languageCombine OOP and FP                                 Scala via Examples                      ...
What is ScalaJVM based languageCombine OOP and FPScalable on architecture and performance                                 ...
Outline1   What is Scala2   GCD Algorithm3   Grep4   Advanced Scala                               Scala via Examples      ...
GCD AlgorithmGreaest Common Divisor (gcd.scala)  def gcd (m: Int, n: Int): Int = (m, n) match {      case (m, ‘m‘)        ...
Compile and Run itMethod 1:scala script-name                                    Scala via Examples                        ...
Compile and Run itMethod 1:scala script-nameMethod 2 (Need main method in object object):scalac script-namescala object-na...
Cases for scala and scalacRunning as scala foo.scala  println("Foo")Running as scalac foo.scala; scala Foo  object Foo {  ...
Declare a methodStart with def keyword                                  Scala via Examples                                ...
Declare a methodStart with def keywordThen function name                                  Scala via Examples              ...
Declare a methodStart with def keywordThen function nameFollowed by optional argument list                                ...
Declare a methodStart with def keywordThen function nameFollowed by optional argument listThen return type follower by a c...
Declare a methodStart with def keywordThen function nameFollowed by optional argument listThen return type follower by a c...
Outline1   What is Scala2   GCD Algorithm3   Grep4   Advanced Scala                               Scala via Examples      ...
GrepGrep (grep.scala)  import scala.io.Source  class Greper(pattern: String, filename: String) {      def execute = {     ...
SingletonThere is no static method in ScalaBut singleton object                                     Scala via Examples    ...
SingletonThere is no static method in ScalaBut singleton objectSingleton object can not be created by new                 ...
SingletonThere is no static method in ScalaBut singleton objectSingleton object can not be created by newSingleton object ...
SingletonThere is no static method in ScalaBut singleton objectSingleton object can not be created by newSingleton object ...
Command Line Arguments  Cmd line arguments will be passed into main function  The main func should be contained in singlet...
Immutable VS. MutableVariable can be declared in two typesval and var                                        Scala via Exa...
Immutable VS. MutableVariable can be declared in two typesval and varval means value, immutablevar means variable, mutable...
Immutable VS. MutableVariable can be declared in two typesval and varval means value, immutablevar means variable, mutable...
List and TupleBasic containers in scala                                    Scala via Examples                             ...
List and TupleBasic containers in scalaList object and its element are immutableList can only contains elements in same ty...
List and TupleBasic containers in scalaList object and its element are immutableList can only contains elements in same ty...
Object is EverythingEvery function is value, and every value is objectHence everything is object                          ...
Object is EverythingEvery function is value, and every value is objectHence everything is object1+2*3/x means 1.+(2.*(3./(...
Object is EverythingEvery function is value, and every value is objectHence everything is object1+2*3/x means 1.+(2.*(3./(...
Outline1   What is Scala2   GCD Algorithm3   Grep4   Advanced Scala                               Scala via Examples      ...
Operators and OrderOperators can be defined by userSome syntactic sugar can be implemented by this                         ...
Operators and OrderOperators can be defined by userSome syntactic sugar can be implemented by thisOperator ends with ‘:’ me...
Case ClassScalas way to allow pattern matching on objects                                                  Scala via Examp...
Curry ExamplePlain-old function  def foo(x: Int, y: Int) = x + y  foo(1, 2)Curried function  def bar(x: Int)(y: Int) = x +...
CurryCurry function bar means two traditional func invocation backto back                                                 ...
CurryCurry function bar means two traditional func invocation backto backFirst func take Int x as parameterAnd return a fu...
CurryCurry function bar means two traditional func invocation backto backFirst func take Int x as parameterAnd return a fu...
Curry  Curry function bar means two traditional func invocation back  to back  First func take Int x as parameter  And ret...
Q AND A          Scala via Examples                               21 / 21
Upcoming SlideShare
Loading in …5
×

20110515 scala by_example

1,144 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,144
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

20110515 scala by_example

  1. 1. Scala via Examples Hu Zi Ming hzmangel@gmail.com 201l-05-15 Scala via Examples 1 / 21
  2. 2. Outline1 What is Scala2 GCD Algorithm3 Grep4 Advanced Scala Scala via Examples 2 / 21
  3. 3. What is ScalaJVM based language Scala via Examples 3 / 21
  4. 4. What is ScalaJVM based languageCombine OOP and FP Scala via Examples 3 / 21
  5. 5. What is ScalaJVM based languageCombine OOP and FPScalable on architecture and performance Scala via Examples 3 / 21
  6. 6. Outline1 What is Scala2 GCD Algorithm3 Grep4 Advanced Scala Scala via Examples 4 / 21
  7. 7. GCD AlgorithmGreaest Common Divisor (gcd.scala) def gcd (m: Int, n: Int): Int = (m, n) match { case (m, ‘m‘) => m case (m, n) if n > m => gcd(n, m) case (m, 0) => m case _ => gcd(n, m % n) } println(gcd(20, 30)) Scala via Examples 5 / 21
  8. 8. Compile and Run itMethod 1:scala script-name Scala via Examples 6 / 21
  9. 9. Compile and Run itMethod 1:scala script-nameMethod 2 (Need main method in object object):scalac script-namescala object-name Scala via Examples 6 / 21
  10. 10. Cases for scala and scalacRunning 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") } Scala via Examples 7 / 21
  11. 11. Declare a methodStart with def keyword Scala via Examples 8 / 21
  12. 12. Declare a methodStart with def keywordThen function name Scala via Examples 8 / 21
  13. 13. Declare a methodStart with def keywordThen function nameFollowed by optional argument list Scala via Examples 8 / 21
  14. 14. Declare a methodStart with def keywordThen function nameFollowed by optional argument listThen return type follower by a colon Scala via Examples 8 / 21
  15. 15. Declare a methodStart with def keywordThen function nameFollowed by optional argument listThen return type follower by a colonEqual sign and method body Scala via Examples 8 / 21
  16. 16. Outline1 What is Scala2 GCD Algorithm3 Grep4 Advanced Scala Scala via Examples 9 / 21
  17. 17. GrepGrep (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 } } Scala via Examples 10 / 21
  18. 18. SingletonThere is no static method in ScalaBut singleton object Scala via Examples 11 / 21
  19. 19. SingletonThere is no static method in ScalaBut singleton objectSingleton object can not be created by new Scala via Examples 11 / 21
  20. 20. SingletonThere is no static method in ScalaBut singleton objectSingleton object can not be created by newSingleton object can not accept arguments Scala via Examples 11 / 21
  21. 21. SingletonThere is no static method in ScalaBut singleton objectSingleton object can not be created by newSingleton object can not accept argumentsSingleton object will be created when first accessed Scala via Examples 11 / 21
  22. 22. Command Line Arguments Cmd line arguments will be passed into main function The main func should be contained in singleton objectjava.lang.NoSuchMethodException: Foo.main is not static object Foo { def main(args: Array[String]) = args.foreach(println) } Scala via Examples 12 / 21
  23. 23. Immutable VS. MutableVariable can be declared in two typesval and var Scala via Examples 13 / 21
  24. 24. Immutable VS. MutableVariable can be declared in two typesval and varval means value, immutablevar means variable, mutable Scala via Examples 13 / 21
  25. 25. Immutable VS. MutableVariable can be declared in two typesval and varval means value, immutablevar means variable, mutableImmutable variable can be changed by referenceLike constant pointer in C/C++ Scala via Examples 13 / 21
  26. 26. List and TupleBasic containers in scala Scala via Examples 14 / 21
  27. 27. List and TupleBasic containers in scalaList object and its element are immutableList can only contains elements in same typeCombine two list means create a new oneElement in list can be accessed via [idx] Scala via Examples 14 / 21
  28. 28. List and TupleBasic containers in scalaList object and its element are immutableList can only contains elements in same typeCombine two list means create a new oneElement in list can be accessed via [idx]Tuple object and its element are immutableTuple can contains elements in different typeTuple using 1-based indexElement in Tuple can be accessed via idx Scala via Examples 14 / 21
  29. 29. Object is EverythingEvery function is value, and every value is objectHence everything is object Scala via Examples 15 / 21
  30. 30. Object is EverythingEvery function is value, and every value is objectHence everything is object1+2*3/x means 1.+(2.*(3./(x))) Scala via Examples 15 / 21
  31. 31. Object is EverythingEvery function is value, and every value is objectHence everything is object1+2*3/x means 1.+(2.*(3./(x)))1::2::3::4::Nil means 1.::(2.::(3.::(4.::(Nil)))) Scala via Examples 15 / 21
  32. 32. Outline1 What is Scala2 GCD Algorithm3 Grep4 Advanced Scala Scala via Examples 16 / 21
  33. 33. Operators and OrderOperators can be defined by userSome syntactic sugar can be implemented by this Scala via Examples 17 / 21
  34. 34. Operators and OrderOperators can be defined by userSome syntactic sugar can be implemented by thisOperator ends with ‘:’ means right associativea * b yields a.*(b)While a ::: b yields b.:::(a) Scala via Examples 17 / 21
  35. 35. Case ClassScalas way to allow pattern matching on objects Scala via Examples 18 / 21
  36. 36. Curry ExamplePlain-old function def foo(x: Int, y: Int) = x + y foo(1, 2)Curried function def bar(x: Int)(y: Int) = x + y bar(1)(2) Scala via Examples 19 / 21
  37. 37. CurryCurry function bar means two traditional func invocation backto back Scala via Examples 20 / 21
  38. 38. CurryCurry function bar means two traditional func invocation backto backFirst func take Int x as parameterAnd return a func value for second func Scala via Examples 20 / 21
  39. 39. CurryCurry function bar means two traditional func invocation backto backFirst func take Int x as parameterAnd return a func value for second funcThis func take the Int y as parameter Scala via Examples 20 / 21
  40. 40. Curry Curry function bar means two traditional func invocation back to back First func take Int x as parameter And return a func value for second func This func take the Int y as parameterFunc declared in other method def bar2_first(x: Int) = (y: Int) => x + y def bar2_second = bar2_first(1) bar2_second(2) Scala via Examples 20 / 21
  41. 41. Q AND A Scala via Examples 21 / 21

×