- 快召唤伙伴们来围观吧
- 微博 QQ QQ空间 贴吧
- 文档嵌入链接
- 复制
- 微信扫一扫分享
- 已成功复制到剪贴板
Introduction to Scala
展开查看详情
1 .Introduction to Scala https://gitter.im/ScalaTaiwan/ScalaTaiwan Speaker - Jimin Hsieh - https://tw.linkedin.com/in/jiminhsieh TA - Pishen Tsai - https://github.com/pishen TA - Vito Jeng - vito@is-land.com.tw TA - Mark Yang - youngce0918@gmail.com Hackpad - https://goo.gl/SIfDk7
2 . Agenda ❖ Why Scala? ❖ Scala concept ❖ Program with Scala ❖ Imperative ❖ Object-Oriented ❖ Functional ❖ Collections ❖ Summary of Scala ❖ Further Reading & Reference ❖ Special Thanks
3 .Why Scala?
4 . Why Scala? “I can honestly say if someone had shown me the Programming in Scala book by Martin Odersky, Lex Spoon & Bill Venners back in 2003 I'd probably have never created Groovy.” James Strachan, creator of Groovy Introduction to Scala, Aleksandar Prokopec
5 . Scala concept ❖ Everything is an object. ❖ Not everything is an object in Java. ❖ There is primitive type in Java. ❖ int, float, boolean, char…etc ❖ Scala is an object-oriented language in pure form: every value is an object and every operation is a method call. ❖ Numeric types and Function are object too. ❖ “+”, “-”, “*”, “/” are methods too.
6 .Scala Class Hierarchy
7 . Scala concept ❖ Everything is expression. ❖ Expression - an instruction to execute something that will return a value. from Wiki ❖ You can also say that an expression evaluates to a result or results in a value. ❖ You will hear evaluation from some of Scala geeks, it means the same thing.
8 . Scala concept ❖ Advanced type system ❖ static ❖ strong ❖ inferred ❖ structural
9 . Scala concept ❖ Avoid to use null. ❖ Less error prone. ❖ NullPointerException ❖ You don’t need to use Null Object pattern.
10 . Program with Scala(Main) object Demo1 { val todayEvent = "JCConf" val workshop = "Introduction to Scala" lazy val fun = (0 to 4).map(x => "fun").mkString(" ") def main(args: Array[String]): Unit = { Object with main method. println("Hello everybody!") print("Welcome to " + todayEvent + "!\n") println("I hope you can enjoy this workshop - " + workshop + ". :P") print("Scala is so much " + fun + "!") } } Demo1
11 . Program with Scala(App) Object with App trait. object Demo2 extends App { val todayEvent = "JCConf" val workshop = "Introduction to Scala" lazy val fun = (0 to 4).map(x => "fun").mkString(" ") println("Hello everybody!") print("Welcome to " + todayEvent + "!\n") println("I hope you can enjoy this workshop - " + workshop + ". :P") print("Scala is so much " + fun + "!") } Demo2
12 . Program with Scala(REPL) ❖ REPL - Read-Evaluate-Print Loop
13 . Program with Scala(REPL) ❖ $ scala -Dscala.color
14 . Program with Scala(Worksheet) ❖ Work with worksheet. ❖ IntelliJ ❖ https://www.jetbrains.com/help/idea/2016.2/ working-with-scala-worksheet.html ❖ Scala IDE or Eclipse with Scala plugin ❖ https://www.youtube.com/watch?v=Forl4hpg7kA
15 . Imperative ❖ var, val, semicolons ❖ If expressions ❖ def ❖ Block expressions ❖ While-Loop ❖ For-Loop ❖ Nested Function ❖ Recursion vs Tail-recursion ❖ Concept of Pattern Matching ❖ Pattern Matching v1
16 . var vs. val ❖ var - variable ❖ val - value ❖ Something that able or ❖ A value is an expression likely to change or be which cannot be changed. Not always evaluated any further. the same. Merriam- Wiki Webster ❖ Opposite to var, val cannot be changed. ❖ It’s similar to final in Java.
17 . Expression with semicolon? val x = 5566 val y = 87 val java = "Java"; val scala = "scala" If you have multiple expressions in one line, you will need semicolon(;). Otherwise you don’t need it. Demo01
18 . If expressions ❖ If has return value.(expression) ❖ Scala have no ternary operator(?:). // Java version final int value = -1; final boolean negative = value < 0 ? true : false; // Scala version val value = 0 val negative = if (value < 0) true else false Everything is an expression. Demo02
19 . def “def” starts a function definition result type of function function name parameter equals sign def max(x: Int, y: Int): Int = { if (x > y) x else function body in curly braces y } Programming in Scala, 3ed by Martin Odersky, Lex Spoon, and Bill Venners Demo03
20 . def def max(x: Int, y: Int): Int = { if (x > y) return x else return y } Demo04
21 . def def max(x: Int, y: Int) = { if (x > y) x No function’s result type else y } Demo05
22 . def def max(x: Int, y: Int) = if (x > y) x else No curly brackets y Demo06
23 . Summary of def ❖ You don’t need return. ❖ Last expression of block will be the return value. ❖ You don’t need return type in method definition. ❖ Scalac will know your return type in most case. ❖ It’s a good habit to have return type, when your API is a public interface. ❖ You don’t need curly bracket. ❖ If you have multiple lines of code, using curly bracket({}) is a good habit.
24 . Block expressions(curly brackets) val n = 5 val factorial = { var result = 1 for (i <- 1 to n) result = result * i result } Last expression(result) in block will be the return value, then it will assign to factorial. Demo07
25 . While-Loop var n = 10 var sum = 0 while (n > 0) { sum = sum + 1 n = n - 1 } Demo08
26 . For-Loop var sum = 0 for (i <- 1 to 10) { sum += 1 } println(sum) Demo09
27 . For-Loop for (i <- 0 until 10) { println(i) } Demo10
28 . For-Loop val n = 5 for {i <- 0 to n j <- 0 to n} { print("*") With curly bracket - “{}”, you can use newlines without semicolon - “;”. if (j == n) println("") } Demo11
29 . Exercise of For-Loop ❖ Print out something like below.