ハイブリッド言語Scalaを使う

4,832 views
4,704 views

Published on

BPStudy #10 : ハイブリッド言語Scalaを使う

0 Comments
8 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,832
On SlideShare
0
From Embeds
0
Number of Embeds
89
Actions
Shares
0
Downloads
55
Comments
0
Likes
8
Embeds 0
No embeds

No notes for slide

ハイブリッド言語Scalaを使う

  1. 1. <moriyoshi@gmail.com>
  2. 2. Java 1.5 Pizza GJ Scala Funnel
  3. 3. public class Application { public static void main(String[] args) { System.out.println(”Hello, World”); } } object Application { def main(args: Array[String]) { println(”Hello, World”) } }
  4. 4. // val numbers = 1.until(10) // 1, 2, ..., 10 println(numbers(5)) // ”6” // println((5).+(3)) // ”8” println(5 + 3) // ”()” ”.” println(5.+(3)) // 5.0+(3) // println(1 until 10)
  5. 5. // // (?)
  6. 6. // String Unit // println puts val puts: String => Unit = println puts(”hey!”) def fun1(x: Int): Int = { def fun2(y: Int): Int = x * y fun2(5) // } val fun3 = (z: Int) => fun1(z + 2) fun3(3) // 25
  7. 7. def matcher(l: List[Int]): Int = l match { case List(1, 2) => 1 case List(2, 3) => 2 case _ => 0 } val l = for { i <- List(1, 2, 3, 4) if (i > 1) } yield i // List(2, 3, 4) l = [1, 2, 3, 4].find_all {|i| i > 1} l = [i for i in (1, 2, 3, 4) if (i > 1)]
  8. 8. object Application { def main(args: Array[String]) { println(”Hello, ” + args(0)) } } scalac $ scalac Application.scala scala $ scala -cp . Application
  9. 9. object Application { def main(args: Array[String]) { println(”Hello, ” + args(0)) } } def : , : , ... + Array[ ] [] ()
  10. 10. object Application { def main(args: Array[String]) { println(”Hello, ” + args(0)) } } object class println
  11. 11. ‣ object class object final class Application$cls private var Application$instance = null final def Application = { if (Application$instance == null) Application$instance = new Application$cls return Application$instance }
  12. 12. ‣ println object import Scala.Predef._
  13. 13. 123 0x123 0123 456L 123.0 13e-8 5e-35D true false ’A’ ’ ’ ’u0041’ ”howdyn” ””ahoy”” ”””I think ”Scala” is great””” (1, 2, 3) <foo>bar</foo>
  14. 14. val var var val : = val
  15. 15. val str: String = ”hey” val i = 123 // val f = 5.0 val longstr = ”””test”test””” val tuple = (1, 2, 3) val xml = <property> <name>prop</name> <value>{longstr}{tuple(0)}</value> </property>
  16. 16. Any AnyVal AnyRef Unit ScalaObject Java Boolean Char Byte Short ... ... Int Long ... ... Float Double ... ... Null Nothing
  17. 17. asInstanceOf[ ] isInstanceOf[ ]
  18. 18. class { } class extends { } class extends with ... { } private class ... {} protected class ... {} sealed class ... {} abstract class ... {} final class ... {}
  19. 19. object { } object extends { } object extends with ... { } private object ... {} protected object ... {}
  20. 20. trait { } trait extends with ... { } trait extends with ... { } private trait ... {} protected trait ... {} sealed trait ... {}
  21. 21. public protected private sealed sealed abstract
  22. 22. class ( ) { val = var = protected val|var ... private val|var ... def this ... def private def ... protected def ... }
  23. 23. def ( ) = def ( ): = def ( ) {...} : , : , ...
  24. 24. class Foo { private var prop_: Int = 0 def prop = prop_ def prop_=(value: Int) = { prop_ = value } } _= val f: Foo = new Foo f.prop = 123 // prop_=()
  25. 25. def this
  26. 26. class Foo { def this(i: Int) = { this() ... ... } } class Bar(param1: Double) { val param1sq = param1 * param1 def this(strParam: String) = { this(strParam.toDouble) } }
  27. 27. trait Named { private var name_ = quot;quot; def name = name_ def name_=(v: String) = { name_ = v } } class Person(firstName: String, lastName: String) extends AnyRef with Named { name = firstName + quot; quot; + lastName } trait SuperMonkeys { val reina = new Person(quot;Reinaquot;, quot;Miyauchiquot;) val lina = new Person(quot;Ritsukoquot;, quot;Matsudaquot;) val mina = new Person(quot;Minakoquot;, quot;Amahisaquot;) val nana = new Person(quot;Nanakoquot;, quot;Takushiquot;) } // extends class with SuperMonkeys { }
  28. 28. [ , ...] [ , ...]
  29. 29. class MyArrayList[T] { private var elems_ = new Array[T](1) private var size_ = 0 def size = size_ def get(index: Int): T = { if (index < 0 || index >= size_) throw new IndexOutOfBoundsException() elems_(index) } def append(elem: T): Unit = { if (size_ >= elems_.length) { val newElems = new Array[T](elems_.length * 2) Array.copy(elems_, 0, newElems, 0, elems_.length) elems_ = newElems } elems_(size_) = elem size_ += 1 var mal = MyArrayList[String]() } mal.append(”hoge”) } mal.append(”fuga”) println(mal.get(0)) // hoge println(mal.get(1)) // fuga
  30. 30. match { case => case => case => }
  31. 31. case class _
  32. 32. class Card {} trait Colored { val color: String } case class NumberedCard(color: String, number: Int) extends Card with Colored {} case class WildCard extends Card {} case class DrawTwo(color: String) extends Card with Colored {} case class DrawFour extends Card {} def putCard = this.discarded match { case NumberedCard(color, number) => 1 case WildCard(color) => 2 case DrawTwo(color) => 3 case DrawFour(color) => 4 }
  33. 33. for ( <- Iterable) { ... ... }
  34. 34. for (a <- (1, 2, 3, 4)) { println(a) } // for (a <- (1, 2, 3, 4); b <- (1, 2, 3, 4)) { println(a) println(b) }
  35. 35. var result = List[Int]() for (i <- List(1, 2, 3, 4)) { result += (i * i) } val result = for (i <- List(1, 2, 3, 4)) yield i * i
  36. 36. val (a, b, c) = (1, 2, 3) var l = List(1, 2, 3) // val l += 4 println(l) // 1, 2, 3, 4 import scala.collection.mutable._ val l = new ListBuffer[Int]() l ++= List(1, 2, 3, 4) l += 5 println(l) // 1, 2, 3, 4, 5
  37. 37. var dict = Map( (”apple”, 100), (”microsoft”, 200), (”banana”, 300) ) dict -= ”microsoft” // ”apple” -> 100, ”banana” -> 300 println(dict)

×