Scala design pattern

3,578 views
3,445 views

Published on

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,578
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
33
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Scala design pattern

  1. 1. Scala Design Pattern @xuwei_k
  2. 2. http://favstar.fm/users/yuroyoro/status/60285682958086145 みんな Scalaのデザパタ しらない?
  3. 3. もう知らないとか言わせない(・ω・´) 7-2章 Scalaのための デザインパターン ● 7-2-1 Loanパターン ● 7-2-2 CONCEPTパターン ● 7-2-3 Cakeパターン ● 7-2-4 バイナリメソッドへの対処 ● 7-2-5 モナドパターンとfor内包表記 ● 7-2-6 Generalized Type Constraints ● 7-2-7 Phantom Typesレビュー加わったぉ ● 7-2-8 型安全なBuilderパターン ● まとめ
  4. 4. みんなもちろん本持ってますよね?
  5. 5. Loanパターン ● 誰もが通る道 ● 「Scala using」とかでググればたぶんいっぱい出てく る ● よって有名なので略(ぉぃ ● 自分が昔書いたもの↓http://d.hatena.ne.jp/xuwei/20100917/1284734307
  6. 6. CONCEPTパターンというより、自分でもどこからどこまでがCONSEPTなのかわかってないから、 なんだかimplicit parameterの使用箇所の解説になってる・・・?(・ω・`)
  7. 7. 突然ですが● Haskellの型クラス(type classes)● C++0xのConcept入るかもしれなかったけど入らないことになっちゃった・・・(´・ω・`) 知ってますか?
  8. 8. Odersky先生 の論文http://ropas.snu.ac.kr/~bruno/papers/TypeClasses.pdf
  9. 9. 1. Introduction2. Type Classes in Haskell 1. Single-parameter type classes 2. Common extensions3. Implicits 1. Implicits in Scala 2. Implicits as the missing link4. The CONCEPT Pattern 1. Concepts: type-class-style interfaces in OO5. Applications and Comparison with Type Classes 1. Ordering concept 2. Abstract data types 3. Statically-typed printf 4. Type class programs are OO programs
  10. 10. 1. Advanced Uses of Type Classes 1. Associated types in GHC Haskell 2. Implicits and type members 3. Session types 4. Arity-polymorphic ZipWith in Scala 5. ZipWith using prioritised overlapping implicits 6. Encoding generalized constraints 7. Type theories using implicits2. Discussion and Related Work 1. Real-world applications 2. Type classes, JavaGI and concepts 3. Generic programming in the large3. Conclusion
  11. 11. 6.6 Encoding generalized constraintsealed abstract class <:< [-S,+T ] extends (S => T)implicit def conforms [A]: A <:< A = new (A <:< A) {def apply (x : A) = x}trait Traversable[T ] {type Coll[X ]def atten [U ] (implicit w : T <:< Traversable[U ]): Coll[U ]}
  12. 12. なんかすごそう Σ(・д・;)
  13. 13. みずしまさんのbloghttp://d.hatena.ne.jp/kmizushima/20110615/1308142330(難しくみせないようにわざとCONSEPTって言葉だしてないけど)これある意味CONSEPTパターンの説明してる?
  14. 14. 代表的な物● scala.math.Equiv● scala.math.PartialOrdering● scala.math.Ordering● scala.math.Numeric● scala.math.Fractional● scala.math.Integral ここからちょっと数学っぽいお話・・・
  15. 15. Equiv (数学的な意味での)同値関係scaladocをコピペ ● A trait for representing equivalence relations. It is important to distinguish between a type that can be compared for equality or equivalence and a representation of equivalence on some type. This trait is for representing the latter. ● An equivalence relation is a binary relation on a type. This relation is exposed as the equiv method of the Equiv trait. The relation must be http://en.wikipedia.org/wiki/Equivalence_relation http://ja.wikipedia.org/wiki/同値関係 http://www.scala-lang.org/api/current/index.html#scala.math.Equiv
  16. 16. Equiv1 reflexive: 反射律equiv(x, x) == true for any x of type T.2 symmetric: 対称律equiv(x, y) == equiv(y, x) for any x and y of type T.3 transitive: 推移律if equiv(x, y) == true and equiv(y, z) == true,then equiv(x, z) == true for any x, y, and z of type T
  17. 17. Ordering 順序付け可能な集合(?)● A trait for representing total orderings. It is important to distinguish between a type that has a total order and a representation of total ordering on some type. This trait is for the latter.● A ordering is a binary relation on a type T that is also an equivalence relation and partial ordering on values of type T. This relation is exposed as the compare method of the Ordering trait. http://en.wikipedia.org/wiki/Total_order http://ja.wikipedia.org/wiki/順序集合http://www.scala-lang.org/api/current/index.html#scala.math.Ordering
  18. 18. OrderingThis relation must be: ● reflexive: 反射律x == x ● antisymmetric: 推移律if x <= y && y <= x, then x == y ● transitive: 反対称律if x <= y && y <= z, then x <= z
  19. 19. PartialOrdering ● A trait for representing partial orderings. It is important to distinguish between a type that has a partial order and a representation of partial ordering on some type. This trait is for representing the latter. ● A partial ordering is a binary relation on a type T that is also an equivalence relation on values of type T. This relation is exposed as the lteqmethod of the PartialOrdering trait.http://en.wikipedia.org/wiki/Partial_order
  20. 20. PartialOrderingThis relation must be: ● reflexive:反射律lteq(x, x) == true, for any x of type T. ● anti-symmetric:反対称律lteq(x, y) == true and lteq(y, x) == truethen equiv(x, y), for any x and y of type T. ● transitive:推移律if lteq(x, y) == true and lteq(y, z) == truethen lteq(x, z) == true,for any x, y, and z of type T.
  21. 21. Numeric 数値Scaladoc何も書いてないェ・・・抽象メソッド達↓def plus(x: T, y: T): Tdef minus(x: T, y: T): Tdef times(x: T, y: T): Tdef negate(x: T): Tdef fromInt(x: Int): Tdef toInt(x: T): Intdef toLong(x: T): Longdef toFloat(x: T): Floatdef toDouble(x: T): Double
  22. 22. Ordering独自に定義してごにょごにょしてみるhttps://gist.github.com/1047689
  23. 23. def sorted [B >: A] (implicit ord: Ordering[B]): List[A]
  24. 24. abstract sealed case class Language(year:Int,name:String)object Scala extends Language(2003,"Scala")object Java extends Language(1995,"Java")object Clojure extends Language(2007,"Clojure")object D extends Language(1999,"D")val languages = List(Java,Scala,Clojure,D)
  25. 25. /** 名前の長さ順 */implicit val nameLengthOrdering = new Ordering[Language]{def compare(x: Language, y: Language) =x.name.length compare y.name.length}languages.sortedList(Language(1999,D), Language(1995,Java), Language(2003,Scala), Language(2007,Clojure))
  26. 26. /** 誕生年順 */implicit val yearOrdering = new Ordering[Language]{def compare(x: Language, y: Language) =x.year compare y.year}language.sortedList(Language(1995,Java), Language(1999,D),Language(2003,Scala), Language(2007,Clojure))
  27. 27. /** 辞書順 */implicit val nameOrdering = new Ordering[Language]{def compare(x: Language, y: Language) = x.name compare y.name}languages.sortedList(Language(2007,Clojure), Language(1999,D), Language(1995,Java), Language(2003,Scala))//Oderingにreverseってメソッドがあるので、明示的に渡して逆順にしてみたりlanguages.sorted(nameOrdering.reverse)List(Language(2003,Scala), Language(1995,Java), Language(1999,D), Language(2007,Clojure))
  28. 28. その他いろいろ( Listの場合 )def sum [B >: A] (implicit num: Numeric[B]): Bdef toMap [T, U] (implicit ev: <:<[A, (T, U)]): Map[T, U]def unzip [A1, A2] (implicit asPair: (A) ⇒ (A1, A2)): (List[A1], List[A2])def unzip3 [A1, A2, A3] (implicit asTriple: (A) ⇒ (A1, A2, A3)): (List[A1],List[A2], List[A3])def flatten [B] (implicit asTraversable: (A) ⇒ TraversableOnce[B]): Traversable[B]def max [B >: A] (implicit cmp: Ordering[B]): Adef maxBy [B] (f: (A) ⇒ B)(implicit cmp: Ordering[B]): Adef min [B >: A] (implicit cmp: Ordering[B]): Adef minBy [B] (f: (A) ⇒ B)(implicit cmp: Ordering[B]): Adef product [B >: A] (implicit num: Numeric[B]): BdefsortBy [B] (f: (A) ⇒ B)(implicit ord: Ordering[B]): List[A]
  29. 29. 全部のデザパタ説明できなかったのでその他のものは● 本読みましょう● 誰か次回(?)発表して
  30. 30. おわり?質問タイム?

×