More Related Content Similar to 楽々Scalaプログラミング Similar to 楽々Scalaプログラミング (20) More from Tomoharu ASAMI (20) 楽々Scalaプログラミング3. ⾃自⼰己
• ⽇日 Java ⻑⾧長 ( ) Lab
•
• XML SmartDoc (XML⽂文 )
• Relaxer (XML/Java )
•
• SimpleModeler (Scala DSL )
• g3 ( )
• g4 (Android )
•
• ⼯工 UML (⽇日 BP)
• ( )
• Scala (Softbank Creative)
5. • 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
8. Scala ⽤用
• ⾼高 ⽣生
• 3
• DSL (Domain Specific Language)
•
• API
• ⾏行行
• Many Core
• Parallel Everything
10. ⾔言 ⻑⾧長
• ⻑⾧長
• ⾼高
• List, ( )
•
• ( ) ( )
( ⼊入 )
•
• ⾏行行
•
• ⼤大
•
• ⼤大
• ⽤用
•
11. ⾔言
• ⾔言 • ⾔言
• pure Lisp • Haskell
• • Scala(+scalaz)
• ⾔言 •
• Lisp, ML, OCaml •
• • Parametric
• ⼿手 polymorphism
•
• •
• Subtype
polymorphism
13. DSL (Domain Specific Language)
• ⾔言 DSL
• ⾼高
• Scala DSL ⽅方
• JJUG CCC 2010 Fall
• http://www.slideshare.net/asami224/scala-dsl
14. 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
16. Future
• ⾮非 ⾏行行 ⾏行行
• ⾏行行 ⾏行行
• ⾏行行 ⾏行行 ⾏行行
• Java (java.util.concurrent)
• Scala (scala.actors)
• Future ⾏行行
17. 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()!
18. Promise
• ⾮非 ⾏行行 ⾏行行
• ⾏行行 ⾏行行
• ⾏行行 ⾏行行 ⾏行行
• Future
• ⾮非 ⾏行行
19. 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!
22. def add1(a: Int): Int = a + 1
add1 (Int) => Int
scala> add1(3)!
res84: Int = 4
scala> 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) => Int
scala> List(1, 2, 3).map(add)!
<console>:34: error: type mismatch;!
found : (Int, Int) => Int!
required: (Int) => ?!
List(1, 2, 3).map(add)!
^!
23. def addc(a: Int)(b: Int): Int = a + b
addc (Int) => (Int) => Int
Int (Int) => Int
scala> 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)!
25. 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))!
}!
26. 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)!
28. def zeroonetwo(a: Int): List[Int] = {!
List(a, a + 1, a + 2)!
}
map
scala> List(1, 2, 3).map(zeroonetwo)!
res138: List[List[Int]] = !
List(List(1, 2, 3), List(2, 3, 4), List(3, 4, 5))
flatMap
scala> List(1, 2, 3).flatMap(zeroonetwo)!
res139: List[Int] = List(1, 2, 3, 2, 3, 4, 3, 4,
5)
29. foldLeft
def 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))!
33. • (referential transparency)
• ⾔言 ⼀一 ⽂文
⾔言
(Wikipedia)
• (persistent data structure)
•
⽣生
⽤用
(Wikipedia)
• Scala
• Value
• scala.collection.immutable
• case class ⽤用
• OOP DTO case class
34. 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")))!
36. 5:
• ⾼高 ⼤大
• Functor ( ⼿手)
• Subgroup ( )
• Monoid ( )
• Monad ( )
• ⾊色
• ( )
• ⾼高
•
• OOP
• Visitor, AbstractFactory, TemplateMethod, Memento
• ⾊色
37. Functor
List(CNN, YAHOO, AMAZON).map(sitelen)
List(86076, 166806, 98089)!
Monad
for (x <- List(1, 2, 3);!
y <- List(10, 20, 30)) yield x * y
List(10, 20, 30, 20, 40, 60, 30, 60, 90)!
Applicative Functor
import 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)!
38. • 1: y = f(x)
• 2:
• 3:
• 4:
• 5: