楽々Scalaプログラミング

4,775 views

Published on

Published in: Technology, Education

楽々Scalaプログラミング

  1. 1. Scala2011 10⽉月17⽇日JJUG, Lab
  2. 2. •  Object-Oriented Programming (OOP) Object- Functional Programming (OFP) •  •  • 
  3. 3. ⾃自⼰己•  ⽇日 Java ⻑⾧長 ( ) Lab•  •  XML SmartDoc (XML⽂文 ) •  Relaxer (XML/Java )•  •  SimpleModeler (Scala DSL ) •  g3 ( ) •  g4 (Android )•  •  ⼯工 UML (⽇日 BP) •  ( ) •  Scala (Softbank Creative)
  4. 4. •  Modegramming Style ( DSL ) •  http://modegramming.blogspot.com/•  SimpleModeler •  http://code.google.com/p/simplemodeler/•  g3 •  http://code.google.com/p/goldenport3/•  g4 •  https://github.com/asami/goldenport-android-library
  5. 5. SCALA ⾔言
  6. 6. Scala
  7. 7. Scala ⽤用• ⾼高 ⽣生 •  3• DSL (Domain Specific Language) •  •  API•  ⾏行行 •  Many Core •  Parallel Everything
  8. 8. ⾔言•  ⾼高 •  • •  ( )
  9. 9. ⾔言 ⻑⾧長•  ⻑⾧長 •  ⾼高 •  List, ( ) •  •  ( ) ( ) ( ⼊入 )•  •  ⾏行行 •  •  ⼤大 •  •  ⼤大 •  ⽤用 • 
  10. 10. ⾔言•  ⾔言 •  ⾔言 •  pure Lisp •  Haskell •  •  Scala(+scalaz)•  ⾔言 •  •  Lisp, ML, OCaml •  •  •  Parametric •  ⼿手 polymorphism •  •  •  •  Subtype polymorphism
  11. 11. DSL (Domain Specific Language)•  ⾔言 DSL •  ⾼高•  Scala DSL ⽅方 •  JJUG CCC 2010 Fall •  http://www.slideshare.net/asami224/scala-dsl
  12. 12. val CNN = "http://www.cnn.com"!val YAHOO = "http://www.yahoo.com"!val AMAZON = "http://www.amazon.com"!!def sitelen(url: String): Int = {! import scala.io.Source! val source = scala.io.Source.fromURL(url)! source.getLines.map(_.length).sum!}!scala> sitelen(CNN)!res92: Int = 85569
  13. 13. List(CNN, YAHOO, AMAZON).map(sitelen).sum ⾏行行 6.3 (scala.testing.Benchmark )List(CNN, YAHOO, AMAZON).par.map(sitelen).sum ⾏行行 2 (scala.testing.Benchmark )
  14. 14. Future•  ⾮非 ⾏行行 ⾏行行 •  ⾏行行 ⾏行行 •  ⾏行行 ⾏行行 ⾏行行•  Java (java.util.concurrent)•  Scala (scala.actors)•  Future ⾏行行
  15. 15. import java.util.concurrent._!!val e = Executors.newSingleThreadExecutor!val f: Future[Int] = e.submit(! new Callable[Int] {! def call() = {! sitelen(CNN)! }! })!!f.get!import scala.actors.Futures._!!val length = future { sitelen(CNN) }!length()!
  16. 16. Promise•  ⾮非 ⾏行行 ⾏行行 •  ⾏行行 ⾏行行 •  ⾏行行 ⾏行行 ⾏行行•  Future •  ⾮非 ⾏行行
  17. 17. import scalaz._!import Scalaz._!!// def sitelen(url: String): Int = ...!def sizekind(len: Int) = {! if (len > 10000) "Large" ! else "Small"!}!def sitelenpromise = (sitelen _).promise!def sizekindpromise = (sizekind _).promise!def sitekindpromise = {! sitelenpromise >=> sizekindpromise!}!scala> val p = sitekindpromise(CNN)!res105: scalaz.concurrent.Promise[java.lang.String] = <promise>!scala> p.get!res107: java.lang.String = Large!
  18. 18. 5
  19. 19. 1: y = f(x)• •  •  ⼤大
  20. 20. def add1(a: Int): Int = a + 1 add1 (Int) => Intscala> add1(3)!res84: Int = 4scala> List(1, 2, 3).map(add1)!res83: List[Int] = List(2, 3, 4)def add(a: Int, b: Int): Int = a + b addx (Int, Int) => Intscala> List(1, 2, 3).map(add)!<console>:34: error: type mismatch;! found : (Int, Int) => Int! required: (Int) => ?! List(1, 2, 3).map(add)! ^!
  21. 21. def addc(a: Int)(b: Int): Int = a + b addc (Int) => (Int) => Int Int (Int) => Intscala> val x = List(1, 2, 3).map(addc)!res87: List[(Int) => Int] = List(<function1>, <function1>, <function1>)!scala> x.map(f => f(1))!res89: List[Int] = List(2, 3, 4)!
  22. 22. 2:• • •  ⼩小•  ⾏行行
  23. 23. case class ()!case class 1()!case class 2()!case class ( 1: 1, !                   2: 2)!!def 1 (a: ) = 1()!def 2 (a: ) = 2()!!def (a: ): = {! ( 1 (a),! 2 (a))!}!
  24. 24. def qsort(a: List[Int]): List[Int] = {! a match {! case Nil => Nil! case List(a) => List(a)! case List(a, b) => if (a < b) List(a, b) ! else List(b, a)! case _ => {! val small = a.filter(_ < a.head)! val large = a.filter(_ > a.head)! qsort(small) ::: List(a.head) ::: qsort(large)! }! }!}!scala> qsort(List(10, 3, 8, 5, 2, 6))!res142: List[Int] = List(2, 3, 5, 6, 8, 10)!
  25. 25. 3:
  26. 26. def zeroonetwo(a: Int): List[Int] = {! List(a, a + 1, a + 2)!} mapscala> List(1, 2, 3).map(zeroonetwo)!res138: List[List[Int]] = !List(List(1, 2, 3), List(2, 3, 4), List(3, 4, 5)) flatMapscala> List(1, 2, 3).flatMap(zeroonetwo)!res139: List[Int] = List(1, 2, 3, 2, 3, 4, 3, 4,5)
  27. 27. foldLeftdef partition5(l: List[Int]) = {! l.foldLeft((List[Int](), List[Int]())) { (xs, x) =>! val (lhs, rhs) = xs! if (x > 5) (lhs, x :: rhs) else (x :: lhs, rhs)! }!}!def partition5(l: List[Int]) = {! ((List[Int](), List[Int]()) /: l) { (xs, x) =>! val (lhs, rhs) = xs! if (x > 5) (lhs, x :: rhs) else (x :: lhs, rhs)! }!}!scala> partition5(List(10, 3, 8, 5, 2, 6))!res130: (List[Int], List[Int]) = !(List(2, 5, 3),List(6, 8, 10))!
  28. 28. 4:
  29. 29. •  (referential transparency) •  ⾔言 ⼀一 ⽂文 ⾔言 (Wikipedia)•  (persistent data structure) •  ⽣生 ⽤用 (Wikipedia)•  Scala •  Value •  scala.collection.immutable •  case class ⽤用 •  OOP DTO case class
  30. 30. case class Person(name: String, email: String)!case class Party(name: String, persons: List[Person])!!val party = Party(! "hobby",! List(Person("Taro", "taro@example.com"),! Person("Hanako", "hanako@example.com")))!
  31. 31. ⽤用•  ⼿手• • • 
  32. 32. 5:•  ⾼高 ⼤大 •  Functor ( ⼿手) •  Subgroup ( ) •  Monoid ( ) •  Monad ( ) •  ⾊色•  ( ) •  ⾼高 • •  OOP •  Visitor, AbstractFactory, TemplateMethod, Memento •  ⾊色
  33. 33. FunctorList(CNN, YAHOO, AMAZON).map(sitelen)List(86076, 166806, 98089)! Monadfor (x <- List(1, 2, 3);! y <- List(10, 20, 30)) yield x * yList(10, 20, 30, 20, 40, 60, 30, 60, 90)! Applicative Functorimport scalaz._!import Scalaz._!List(10, 20, 30) <*> (List(1, 2, 3) <*>! ((_: Int) * (_: Int)).curried.pure[List])List(10, 20, 30, 20, 40, 60, 30, 60, 90)!
  34. 34. •  1: y = f(x)•  2:•  3:•  4:•  5:
  35. 35. END

×