Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

ゼロから始めるScala文法

515 views

Published on

初心者向けScala文法の資料です (@Scala関西Summit 2017)

Published in: Software
  • Be the first to comment

ゼロから始めるScala文法

  1. 1.           2
  2. 2.                 3
  3. 3.                     4
  4. 4.                           5
  5. 5.                 6
  6. 6.     7
  7. 7. object Main { def main(args: Array[String]): Unit = { val message1 = "Hello" var message2 = "World" message2 += "!" println(message1 + " " + message2) } } 8
  8. 8. def main(args: Array[String]): Unit = { val message1 = "Hello" var message2 = "World" message2 += "!" println(message1 + " " + message2) } [] Unit void 9
  9. 9. { val message1 = "Hello" var message2 = "World" message2 += "!" println(message1 + " " + message2) } 10
  10. 10. def twice(x: Int): Int = { x * 2 } def twice(x: Int): Int = x * 2 {} 11
  11. 11. val result = { val x = 1 + 2 if (x < 0) x else x * 2 } 12
  12. 12. val result = { val x = 1 + 2 if (x < 0) x else x * 2 } 13
  13. 13. for {i <- 1 to 3} { println(i) } for {i <- 1 to 3} println(i) val results = for {i <- 1 to 3} i 14
  14. 14. val results1 = for {i <- 1 to 3} yield i val results2 = for { i <- 1 to 5 by 2 j <- 2 to 1 by -1 } yield { val x = i * j x + 1 } 15
  15. 15. val n = 2 val result = n match { case 1 => "1" case 2 | 3 => "2 or 3" case x if x % 2 == 0 => " " + x.toString case x => s" ${x}" } 16
  16. 16. val n = 5 val result = n match { case 1 => "1" case 2 | 3 => "2 or 3" case x if x % 2 == 0 => " " + x.toString case x => s" ${x}" } 17
  17. 17. class Triangle(edges: Array[Int]) { val area = ??? // def printFigure(): Unit = println(" ") } val triangle = new Triangle(Array(3, 4, 5)) println(triangle.area) 18
  18. 18. abstract class Polygon(edges: Array[Int]) { val n = edges.size val area: Double def printFigure(): Unit } class Triangle(edges: Array[Int]) extends Polygon(edges) { val area = ??? // def printFigure(): Unit = println(" ") } val triangle = new Triangle(Array(3, 4, 5)) println(triangle.area) 19
  19. 19. abstract class Polygon private(edges: Array[Int]) { val n = edges.size val area: Double def printFigure(): Unit } object Polygon { def fromEdges(edges: Array[Int]): Polygon = edges.size match { case 3 => new Triangle(edges) case 4 => new Square(edges) case n => ??? } } Polygon.fromEdges(Array(3, 4, 5)) 20
  20. 20. import java.util.Date trait Tag { val createdDate: Date } trait Color { val rgb: RGB } class TaggedGreenTriangle extends Triangle with Tag with Color { val rbg = new RGB(0, 255, 0) val createdDate = new Date } 21
  21. 21. val list1 = (1 to 3).toList val list2 = List(1, 2, 3) val array = Array(1, 2, 3) val odds1 = list1.filter(x => x % 2 == 1) val odds2 = array.filter(x => x % 2 == 1) val sum = list1.sum val twice = list1.map(x => x * 2) val sorted = list1.sorted 22
  22. 22. val list = (1 to 3).toList val odds1 = list.filter(x => x % 2 == 1) val selectOdd: Int => Boolean = x => x % 2 == 1 val odds2 = list.filter(selectOdd) 23
  23. 23. def getData(q: Query): Option[String] = ??? getData(q) match { case Some(str) => println(str) case None => println(" ") } 24
  24. 24. def getNullableString(): String = ??? // val stringOpt = Option(getNullableString()) stringOpt match { case Some(str) => println(str) case None => println("null ") } def throwableMethod(): Int = ??? // val result = try { throwableMethod() } catch { case ex: Throwable => 0 } 25
  25. 25. val list = (1 to 3).toList list.foreach(x => println(x)) def getNullableString(): String = ??? // val stringOpt = Option(getNullableString()) stringOpt.foreach(str => println(str)) 26
  26. 26. 27
  27. 27. val x1 = 1 + 2 val x2 = 1.+(2) implicit class MyInt(val x: Int) extends AnyVal { def add(another: Int): Int = x + another } val y1 = 1.add(2) val y2 = 1 add 2 28
  28. 28. class Cat(cry: String) { def apply(postfix: String): Unit = println(cry + postfix) } object Cat { def apply(cry: String): Cat = new Cat(cry) } val cat1 = new Cat(" ") val cat2 = Cat(" ") val cat3 = Cat.apply(" ") cat1("!") cat1.apply("!") 29
  29. 29. class Cat(cry: String) val cat = new Cat(" ") // cry private class Cat(val cry: String) val cat = new Cat(" ") // val println(cat.cry) 30
  30. 30. case class Cat(cry: String) val cat1 = new Cat(" ") val cat2 = Cat(" ") val cat3 = Cat.apply(" ") println(cat1.cry) println(cat1.toString) getData(q) match { case Some(str) => println(str) case None => println(" ") } 31

×