[Start] Scala

6,958 views

Published on

1 Comment
3 Likes
Statistics
Notes
No Downloads
Views
Total views
6,958
On SlideShare
0
From Embeds
0
Number of Embeds
4,691
Actions
Shares
0
Downloads
12
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide

[Start] Scala

  1. 1. println("Hello World!")
  2. 2. Do you knowObject-oriented Programming?
  3. 3. Do you know FunctionalProgramming?
  4. 4. Do you know Scala?’’’
  5. 5. [Start] Scala!’’’ @mumoshu
  6. 6. # of atnders php java scala jspython perl 0 5 10 15
  7. 7. Categorize
  8. 8. PHP, Java, ScalaWeb dev General Purpose Scala PHP Java
  9. 9. PHP, Java, ScalaWeb dev General PurposeDynamically Statically typedtyped Scala PHP Java
  10. 10. PHP, Java, ScalaWeb dev General Purpose Shorter codeDynamically Statically typedtyped Scala Longer code PHP Java
  11. 11. Scala• has NOTHING NEW!
  12. 12. PHP and Scala• PHP + Static Typing + Shorter code = Scala ?!
  13. 13. Java and Scala• Java + Shorter code = Scala ?!
  14. 14. Really short?
  15. 15. Basic computation• Calculate the sum in imperative style
  16. 16. Basic computation• Calculate the sum in imperative style 1 + 2 + 3 = 6
  17. 17. Basic computation• Calculate the sum in imperative style 1 + 2 + 3 = 6 sum = 0 sum = sum + 1 sum = sum + 2 sum = sum + 3
  18. 18. MY Ranking1. (Ruby)2. Scala3. (JavaScript)4. PHP5. Java
  19. 19. Sum with Javaint[] array = new int[] { 1, 2, 3 };int sum = 0;for (int i=0; i<array.length; i++) {  sum += array[i];}System.out.println(sum);
  20. 20. Sum with Javaint[] array = new int[] { 1, 2, 3 };int sum = 0;for (int i=0; i<array.length; i++) {  sum += array[i];}System.out.println(sum);
  21. 21. int[] int[] int int
  22. 22. Sum with PHP$array = array(1, 2, 3);$sum = 0;for ($i=1; $i<=count($array); $i++) {    $sum += $array[$i];}print $sum;
  23. 23. Sum with PHP$array = array(1, 2, 3);$sum = 0;for ($i=1; $i<=count($array); $i++) {    $sum += $array[$i];}print($sum);
  24. 24. $$$$$$$$
  25. 25. Sum with JSvar array = [1,2,3]var sum = 0for (var i in array) { sum += array[i]}console.log(sum);
  26. 26. Sum with JSvar array = [1,2,3]var sum = 0for (var i in array) { sum += array[i]}console.log(sum);
  27. 27. (‘A`) vaaaaaaaar
  28. 28. Sum with Rubyarray = [1, 2, 3]sum = 0for (x in array) sum += xendputs sum
  29. 29. (^o^) Elegant!
  30. 30. Scala + forvar array = Array(1, 2, 3)var sum = 0for (x <- array) {  sum += x}println(sum)
  31. 31. (‘-’) Is that your power?
  32. 32. Scala + forvar array = Array(1, 2, 3)var sum = 0for (x <- array) {  sum += x}println(sum)
  33. 33. Scala + forvar array = Array(1, 2, 3)var sum = 0for (x <- array) {  sum += x}println(sum)
  34. 34. Scala + foreachvar array = Array(1, 2, 3)var sum = 0array foreach { x =>  sum += x}println(sum)
  35. 35. Scala + foreachvar array = Array(1, 2, 3)var sum = 0array foreach { x =>  sum += x}println(sum)
  36. 36. Scala + foreachvar array = Array(1, 2, 3)var sum = 0array foreach { sum += _}println(sum)
  37. 37. Sum with `reduce`• Calculate the sum in functional styl
  38. 38. Sum with `reduce`• Calculate the sum in functional style sum = 1 + 2 + 3 sum = 3 + 3 sum = 6
  39. 39. Sum with `reduce`• Calculate the sum in functional style sum = 1 + 2 + 3
  40. 40. Sum with `reduce`• Calculate the sum in functional style 1. (Ruby) 2. Scala 3. (JavaScript) 4. PHP 5. Java
  41. 41. JavaNo reduce
  42. 42. PHP$array = array(1, 2, 3);function reduce($a, $b){ return $a + $b;};$sum = array_reduce($array, reduce);print $sum;
  43. 43. JavaScriptvar array = [1, 2, 3];var sum = array.reduce(function(a,b){ return a + b});console.log(sum);
  44. 44. `for` is shorter...
  45. 45. Rubyarray = [1, 2, 3]sum = array.reduce(&:+)puts sum
  46. 46. Scalaval array = Array(1, 2, 3)val sum = array.reduce(_+_)println(sum)
  47. 47. (^-^)
  48. 48. One more thing...
  49. 49. More Scalaval array = Array(1, 2, 3)val sum = array.sumprintln(sum)
  50. 50. You learn everyday• And you code gets BETTER
  51. 51. Are you• a Java engineer?• or maybe a PHP engineer?• Do you use `Class’?
  52. 52. Class• Define a class in 1. Scala 2. (Ruby) 3. PHP 4. Java 5. (JavaScript)
  53. 53. Greeter Hello ScalaGreeter
  54. 54. Classes in Javapublic class Greeter { private String name; public Greeter(String name) { this.name = name; } public Greeter() { Greeter(“Scala”); } public String getName() { return this.name; } public void greet() { System.out.println(“Hello “ + this.name); }}Greeter greeter = new Greeter("Scala!");greeter.greet();
  55. 55. Classes in PHP<?phpclass Greeter { private $name; function __construct($name = Scala) { $this->name = $name; } public function getName() { return $this->name; } public function greet() { print ‘Hello ‘ . $this->name; }}$greeter = new Greeter(‘Scala!’);$greeter->greet();?>
  56. 56. Classes in JSfunction Greeter(name) { this.name = typeof name == “undefined” ? “Scala” : name;}Greeter.prototype = { getName: function() { return this.name; }, greet: function() { console.log(“Hello “ + this.name); }};var greeter = new Greeter(“Scala”);greeter.greet();
  57. 57. Classes in Rubyclass Greeter attr_reader :name def initialize(name="Scala") @name = name end def greet puts “Hello “ + @name endendgreeter = Greeter.new(“Scala”)greeter.greet
  58. 58. Classes in Scalaclass Greeter(val name: String = “Scala”) { def greet() { println(“Hello “ + name) }}val greeter = new Greeter("Scala!");greeter.greet()
  59. 59. Really short?
  60. 60. YES!!!Really short?
  61. 61. break 1/2   ビースト★ハーレム ∼野獣の甘噛み∼http://pf.gree.jp/56616
  62. 62. Overview
  63. 63. Scala is• General purpose • Not only Web• Multi-paradigm • Object-oriented + Functional programming
  64. 64. Scala ’’’• Run on JVM• A lot of Java libraries• Hybrid language • Imperative programming • Object oriented programming • Functional programming
  65. 65. Whats happy with FP• Concurrency• Scalability
  66. 66. Imperative vs Functional• Imperative Programming do A -> state++, do B -> state++, ...• Functional programming in1 -> f(x) -> out1 in2 -> g(x) -> out2
  67. 67. + concurrency
  68. 68. Easy concurrency• Imperative programming do A -> state <- do B > sudden conflict!!! <• Functional programming in1 -> f(x) -> out1, in2 -> g(x) -> out2 > no conflict!!! <
  69. 69. Dynamic vs Static typing• About type-safety
  70. 70. Dynamic typing• GOOD • Run anyway• BAD • More runtime errors
  71. 71. NullPointerException
  72. 72. ReferenceError
  73. 73. TypeError
  74. 74. Call to undefined function
  75. 75. Static typing• GOOD • More compile errors• BAD • Hard to read error messages
  76. 76. Scala 2.10• We have Scala 2.9.3• Scala 2.10 is coming (on 12/26?)
  77. 77. Recommendation• If you want to: • write something other than Web app • write noise-free, consise code • develop your skill GRADUALLY
  78. 78. [Start] Scala!’’’ @mumoshu
  79. 79. Thank you!
  80. 80. Our bible
  81. 81. Scala for business• Scalaはお仕事に向いてます • Java同様、静的型付けは複数人での 開発に有効 • Scalaはエンジニアの成長を止めない
  82. 82. Scala for education• ステップアップ学習に最適 • sumの例のように、Scalaに慣れてくる と徐々に簡潔な書き方が出来るよう になっていく • 手続き的に書ける。慣れたらいつで も関数型チックに書ける。
  83. 83. Scala’s flexibility• Scalaは自由度が高い • 同じ意味のことを幾通りにも書けて メンテナンス性が悪い、のではない • 各人のレベルにあった記述ができる • プログラマの創造性を阻害しない
  84. 84. Learning Scala• Javaと同じ書き方でいいなら、Javaと同じ学 習コスト • ちょっと短いだけのJavaとして書けるので• Web開発ならPHPの方が最初の敷居が低い
  85. 85. REPL• Scalaの対話環境• ScalaのREPLを起動すると”Welcome to Scala”って言われる• 「ようこそScalaへ。ご命令を。」って 言ってくれるScalaかわいい
  86. 86. Compilation time• Scalaのコンパイルは長い• Time is money• PCをスペックアップしましょう

×