Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Scala for Java Programmers (Short Edition)

Scala for Java Programmers (Short Edition)

JavaプログラマのためのScala入門 (Short Edition) v1.1

目次:
- Scala とは?
- case クラス
- パターンマッチ
- コレクション
-- コレクション演算
-- 不変コレクション
-- 並列コレクション
- Option 型

Ef45a84b10aa3cb5acd2ea0c9846bd3b?s=128

Yuta Okamoto

March 10, 2013
Tweet

Transcript

  1. JavaϓϩάϥϚͷͨΊͷ Scalaೖ໳ Ԭຊ ༤ଠ (@okapies) 2013/03/10 (Ver 1.1) Short Edition

  2. ͜ͷൃද͸… ҎԼͷΑ͏ͳํ͕ର৅Ͱ͢ɻ • Java ʹΑΔιϑτ΢ΣΞ։ൃΛҰ௨Γܦݧ͍ͯ͠Δɻ • ձ࿩Ͱಥવ ”̋̋ύλʔϯ” ͱ͍͏୯ޠ͕ग़͖ͯͯ΋ɺ ڍಈෆ৹ʹͳΒͳ͍ఔ౓ͷ஌͕ࣝ͋Δɻ

    • ؔ਺ܕϓϩάϥϛϯάͷ࿩͸͠·ͤΜʂ
  3. Short Edition? • ౰ॳ͸ɺJava ಉ౳ͷػೳ → Scala ಛ༗ͷػೳ·ͰΛ ࡾষߏ੒Ͱ঺հ͍ͯ͘͠༧ఆͩͬͨɻ •

    …͕ɺҰষͷ࣌఺Ͱϖʔδ਺͕ੌ͍ࣄʹͳΓஅ೦ɻ • ࠓճ͸ɺ໌೔࢖͑ΔศརػೳΛηϨΫτͯ͠͝঺հɻ
  4. ໨࣍ • Scala ͱ͸ʁ • case Ϋϥε • ύλʔϯϚον •

    ίϨΫγϣϯ • Option ܕ
  5. Scalaͱ͸ʁ • Java VM ্Ͱಈ࡞͢Δϓϩάϥϛϯάݴޠɻ • 2003 ೥ʹ࠷ॳͷόʔδϣϯ͕ϦϦʔε͞Εͨɻ • ࠷৽൛͸

    2.10ʢ2013 ೥ 3 ݄ݱࡏʣɻ • εΠε࿈๜޻Պେֶϩʔβϯψߍͷ Martin Odersky ڭत͕։ൃɻ • Java 5 ͷ Generics ΍ݱߦੈ୅ͷ javac ͷ։ൃʹܞΘͬͨਓ෺ɻ • 2011 ೥ʹ Scala ͷαϙʔτ΍ίϯαϧΛߦ͏ Typesafe ࣾΛઃཱɻ
  6. Scalaͷಛ௃ - 1 • ʮϚϧνύϥμΠϜʯͷ੩తܕ෇͖ݴޠ • ΦϒδΣΫτࢦ޲ϓϩάϥϛϯάͱ ؔ਺ܕϓϩάϥϛϯάͷೋͭͷύϥμΠϜΛ༥߹ɻ • རศੑ΍֦ுੑͷ޲্ͨ͠

    ”Better Java” Ͱ΋͋Δɻ
  7. Scalaͷಛ௃ - 2 • εέʔϥϒϧͳݴޠ (SCAlable LAnguage) • খ͞ͳεΫϦϓτ͔Βେ͖ͳγεςϜ·Ͱهड़Մɻ •

    υϝΠϯಛԽݴޠ (DSL) Λߏங͢ΔͨΊͷ༷ʑͳػ ೳ͕αϙʔτ͞Ε͍ͯΔɻ
  8. ࠾༻ࣄྫ • ΢ΣϒαʔϏε • TwitterɺLinkedInɺFoursquareɺTumblrɺυϫϯΰ • ۚ༥ɺ঎඼औҾ • UBS ۜߦɺEDF

    Tradingɺࡾඛ UFJ IT • NASAʢʂʣ ࠃ಺ࣄྫ΋૿Ճதʂ
  9. ຊ୊ʹೖΔલʹ…

  10. REPLΛ࢖͓͏ʂ • REPL: Read-Eval-Print Loop • Scala ʹඪ४Ͱ෇ଐ͢Δର࿩ܕͷ࣮ߦ؀ڥɻ • ίϯιʔϧʹଧͪࠐΜͩίʔυΛͦͷ৔Ͱ࣮ߦ͠ɺ

    ݁ՌΛ͔֬ΊΔ͜ͱ͕Ͱ͖Δɻ • खΛಈ͔͠ͳ͕Β Scala Λֶश͢Δͷʹ࠷దɻ
  11. $ scala Welcome to Scala version 2.10.0 (Java HotSpot(TM) 64-Bit

    Server VM, Java 1.6.0_37). Type in expressions to have them evaluated. Type :help for more information. scala> 1 + 2 res0: Int = 3 scala> println("1 + 2 = " + res0) 1 + 2 = 3 scala> :quit
  12. case Ϋϥε case Ϋϥε

  13. ͬͦ͘͞Ͱ͕͢… case Ϋϥε

  14. Java Ͱ σʔλΫϥεΛ ࣮૷͢Δͷͬͯ ໘౗͡Όͳ͍Ͱ͔͢ʁ case Ϋϥε

  15. public class Person { private String name = null; private

    int age = 0; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public int getAge() { return this.age; } public void setAge(int age) { this.age = age; } } Java Person - name: String - age: int case Ϋϥε
  16. Կ͕໰୊͔ʁ • େྔͷ”ϘΠϥʔϓϨʔτ”ͳίʔυ • ৽͍͠ΫϥεΛ࣮૷͢ΔͨΊͷίετ͕ߴ͍ɻ • ”࣮૷ͷҙਤ”͕ຊ࣭తͰͳ͍෦෼ʹຒ΋Εͯ͠·͏ɻ Person - name:

    String - age: int case Ϋϥε ɾɾɾ
  17. Scala Ͱ ॻ͍ͯΈΔͱ…ʁ case Ϋϥε

  18. class Person(var name: String, var age: Int) Person - name:

    String - age: int Scala ͨͬͨҰߦʂ ˍ Ϋϥεͷҙਤ͕໌֬ʂ var/val Λ͚ͭΔ case Ϋϥε
  19. class Person(var name: String, var age: Int) Scala class Person(name:

    String, age: Int) { def name: String = name def name_=(name: String) { this.name = name } def age: Int = age def age_=(age: Int) { this.age = age } } getter setter ίϯετϥΫλ ʹ ΞΫηα͕ࣗಈతʹఆٛ͞ΕΔ ʢຊ౰ͷ Scala ίʔυͰ͸ͳ͍͚Ͳɺงғؾ͸͜Μͳײ͡ͱ͍͏͜ͱͰʣ case Ϋϥε
  20. val p = new Person(“Tom Green”, 25) println(“age: ” +

    p.age) // p.age() p.name = “Martin Odersky” // p.name_=(“Martin Odersky”) Scala ϑΟʔϧυͱಉ͡จ๏Ͱ ࢀরɾߋ৽Ͱ͖Δ ʢ࣮ࡍͷॲཧ͸ getter/setter Λܦ༝͢ΔͷͰ ΧϓηϧԽ͕कΒΕΔʣ case Ϋϥε
  21. ͦ͏͍͑͹… case Ϋϥε

  22. toString ͱ͔ equals ͱ͔ hashCode ͷ࣮૷΋ ໘౗͡Όͳ͍Ͱ͔͢ʁ case Ϋϥε

  23. public class Person { private String name = null; private

    int age = 0; ... public boolean equals(Object obj) { if (obj == null) return false; if (obj instanceof Person) { Person rhs = (Person) obj; if (this.name == null) return rhs.name == null; return this.name.equals(rhs.name) && this.age == rhs.age; } return super.equals(obj); } public int hashCode() { return 31 * this.name.hashCode() + this.age; } public String toString() { return “Person(” + this.name + “,” + this.age + “)”; } } Java case Ϋϥε ਖ਼࣮͘͠૷͢Δͷ͸େม…
  24. ͦ͜Ͱ case Ϋϥε case Ϋϥε

  25. case class Person(var name: String, var age: Int) Scala એݴʹ

    case ͱॻ͚ͩ͘ʂ case Ϋϥε
  26. case class Person(var name: String, var age: Int) val p1

    = Person(“Tom Green”, 25) val p2 = Person(“Martin Odersky”, 54) p1.equals(p2) hashSet.add(p1) // depends on hashCode() println(p1.toString()) // “Person(Tom Green,25)” Scala શͯࣗಈతʹ࣮૷͞ΕΔ case Ϋϥε
  27. Scala Λ࢖͏ͱ ”ܕ”Λ؆୯ʹ ఆٛͰ͖Δ case Ϋϥε

  28. case Ϋϥε from https://github.com/twitter/finagle/ ྫ: memcached ͷϓϩτίϧఆٛ େྔͷܕఆ͕ٛඞཁ

  29. case Ϋϥε from https://github.com/twitter/finagle/ ྫ: memcached ͷϓϩτίϧఆٛ େྔͷܕఆ͕ٛඞཁ ΫϥεΛ؆͔ܿͭݟ௨͠ྑ͘هड़Ͱ͖Δ 㱺

    ”ܕͷྗ”Λ؆୯ʹҾ͖ग़ͤΔʂ
  30. ·ͱΊ • Scala ʹ͸σʔλΫϥε༻ͷ౶ҥߏจ͕͋Δɻ • ίϯετϥΫλ΍ getter/setter Λࣗಈతʹఆٛ͢Δ ౶ҥߏจɻ •

    equals() ౳Λࣗಈతʹ࣮૷͢Δ case Ϋϥεɻ • case Ϋϥεͷ͝རӹ͸ଞʹ΋ɻৄ͘͠͸ޙ΄Ͳ… case Ϋϥε
  31. ύλʔϯϚον ύλʔϯϚον

  32. ύλʔϯϚον ௒ڧԽ൛ switch จ ʹ ύλʔϯϚον

  33. switch (ch) { case ‘ ’: buf.append(‘-’); break; case ‘¥n’:

    buf.append(‘:’); break; default: buf.append(ch); break; } Java •Java ͷ switch จͷෆศͳͱ͜Ζ •੔਺஋ͱจࣈྻ͔͠ൺֱͰ͖ͳ͍ɻ •ൺֱΞϧΰϦζϜ͕”Ұக”͔͠࢖͑ͳ͍ɻ ύλʔϯϚον
  34. Scala ͷ match ࣜʹΑΔ ύλʔϯϚον ύλʔϯϚον

  35. ch match { case ‘ ‘ => buf.append(‘-’) case ‘¥n’

    => buf.append(‘:’) case _ => buf.append(ch) } Scala switch (ch) { case ‘ ’: buf.append(‘-’); break; case ‘¥n’: buf.append(‘:’); break; default: buf.append(ch); break; } Java ߏจ͸ࣅ͍ͯΔ •switch ͷ୅ΘΓʹ match Λ࢖͏ɻ •۠੾Γه߸͸ => Λ࢖͏ɻ •ϑΥʔϧεϧʔ͸͠ͳ͍ɻ break จ͸ෆཁ ύλʔϯϚον
  36. match ࣜͰ͔͠ Ͱ͖ͳ͍͜ͱ ύλʔϯϚον

  37. abstract class Tree case object Empty extends Tree case class

    Node( elem: Int, left: Tree, right: Tree) extends Tree Tree Empty - elem: Int - left: Tree - right: Tree ྫ: ೋ෼໦ Node ύλʔϯϚον
  38. abstract class Tree case object Empty extends Tree case class

    Node( elem: Int, left: Tree, right: Tree) extends Tree def traverse(t: Tree) = t match { case empty: Empty => Nil case node: Node => traverse(node.left) ::: List(node.elem) ::: traverse(node.right) } Scala ύλʔϯϚον ஋ͷܕΛൺֱͰ͖Δ Node ͔Βதͷ஋ΛऔΓग़͢ࡍʹɺ Tree ͔Β Node ΁ͷΩϟετ͕ෆཁͳ͜ͱʹ஫໨ʂ
  39. abstract class Tree case object Empty extends Tree case class

    Node(elem: Int, left: Tree, right: Tree) extends Tree def traverse(t: Tree) = t match { case Empty => Nil case Node(e, l, r) => traverse(l) ::: List(e) ::: traverse(r) } Scala ύλʔϯϚον த਎Λநग़Ͱ͖Δʂ ஋ͷநग़͸ case Ϋϥεͷػೳͷͻͱͭ ίϯετϥΫλͱಉ͡ه๏ʂ
  40. ೖΕࢠʹͳ͍ͬͯͯ΋ ෼ղͰ͖Δ t match { case Node(e1, Node(e2, Empty, Empty),

    Node(e3, Empty, Empty)) => ... case ... } Scala ύλʔϯϚον e1 e2 E e3 E E E
  41. case class Person(name: String, age: Int) def isAdult(p: Person) =

    p match { case Person(“Martin Odersky”, _) => true case Person(_, age) if (age >= 18) => true case _ => false } Scala ύλʔϯϚον ෳࡶͳ৚݅෼ذ΋ ؆ܿʹهड़Ͱ͖Δ ໊લ͕ Ұக ͨ͠Β true 18 ࡀ Ҏ্ ͳΒ true _ ͸ϫΠϧυΧʔυ
  42. GoF ͷ Visitor ύλʔϯ ύλʔϯϚον

  43. Visitor ύλʔϯ͸…ΞϧΰϦζϜΛΦϒδΣΫτͷߏ ଄͔Β෼཭͢ΔͨΊͷσβΠϯύλʔϯͰ͋Δɻ෼཭ ʹΑΔ࣮༻తͳ݁Ռͱͯ͠ɺطଘͷΦϒδΣΫτʹର ͢Δ৽ͨͳૢ࡞Λߏ଄Λมߋͤͣʹ௥Ճ͢Δ͜ͱ͕Ͱ ͖Δɻ from http://ja.wikipedia.org/wiki/Visitor_ύλʔϯ ύλʔϯϚον

  44. class CarElementPrintVisitor implements CarElementVisitor { public void visit(Wheel wheel) {

    System.out.println("Visiting " + wheel.getName() + " wheel"); } public void visit(Engine engine) { System.out.println("Visiting engine"); } public void visit(Body body) { System.out.println("Visiting body"); } public void visit(Car car) { System.out.println("Visiting car"); } } class CarElementDoVisitor implements CarElementVisitor { public void visit(Wheel wheel) { System.out.println( "Kicking my " + wheel.getName() + " wheel"); } public void visit(Engine engine) { System.out.println("Starting my engine"); } public void visit(Body body) { System.out.println("Moving my body"); } public void visit(Car car) { System.out.println("Starting my car"); } } ύλʔϯϚον from http://en.wikipedia.org/wiki/Visitor_pattern ͳʹ͜Ε͜Θ͍ (ʀʉʔʉ) interface CarElementVisitor { void visit(Wheel wheel); void visit(Engine engine); void visit(Body body); void visit(Car car); } interface CarElement { void accept(CarElementVisitor visitor); } class Wheel implements CarElement { private String name; public Wheel(String name) { this.name = name; } public String getName() { return this.name; } public void accept(CarElementVisitor visitor) { visitor.visit(this); } } class Engine implements CarElement { public void accept(CarElementVisitor visitor) { visitor.visit(this); } } class Body implements CarElement { public void accept(CarElementVisitor visitor) { visitor.visit(this); } } class Car implements CarElement { CarElement[] elements; public Car() { ... } public void accept(CarElementVisitor visitor) { for(CarElement elem : elements) { elem.accept(visitor); } visitor.visit(this); } } Java
  45. ύλʔϯϚονͳΒ Visitor ύλʔϯ͸ ෆཁʂ ύλʔϯϚον

  46. abstract class CarElement case class Wheel(name: String) extends CarElement case

    object Engine extends CarElement case object Body extends CarElement case class Car(elements: List[CarElement]) extends CarElement def print(elem: CarElement) = elem match { case Wheel(name) => println(“Visiting” + name + “ wheel”) case Engine => println(“Visiting engine”) case Body => println(“Visiting body”) case Car(elements) => elements foreach print; println(“Visiting car”) } def doSomething(elem: CarElement) = elem match { case Wheel(name) => println(“Kicking my ” + name + “ wheel”) case Engine => println(“Starting my engine”) case Body => println(“Moving my body”) case Car(elements) => elements foreach doSomething; println(“Starting my car”) } Scala ύλʔϯϚον
  47. ·ͱΊ • ύλʔϯϚονΛ࢖͏ͱɺ ҎԼͷػೳΛಉ࣌ʹ૊Έ߹Θͤͳ͕Β؆ܿʹهड़Ͱ͖Δɻ • ৚݅෼ذʢҰகൺֱɺ͚ͩ͡Όͳ͍ʣ • ܕͷൺֱʢΩϟετෆཁʣ • த਎ͷநग़ʢcase

    Ϋϥεͷ͝རӹʂʣ • ύλʔϯϚον͸ Visitor ύλʔϯΛஔ͖׵͑ΒΕΔɻ ύλʔϯϚον
  48. ίϨΫγϣϯ ίϨΫγϣϯ

  49. Scala ඪ४ίϨΫγϣϯϥΠϒϥϦͷಛ௃ 1.ڧྗͳίϨΫγϣϯԋࢉɻ 2.ෆม(immutable)ίϨΫγϣϯɻ 3.؆୯ʹฒྻॲཧ͕Ͱ͖Δɻ ίϨΫγϣϯ

  50. ίϨΫγϣϯԋࢉ ίϨΫγϣϯ

  51. ૣ଎Ͱ͕͢໰୊Ͱ͢ ίϨΫγϣϯ

  52. public String func(List<Person> persons) { List<Person> filtered = new ArrayList<Person>();

    for (Person p : persons) { if (p.getAge() >= 18) { filtered.add(p); } } StringBuffer buf = new StringBuffer(); for (Person p : filtered) { buf.append(p.getName() + "," + p.getAge() + "\n"); } return buf.toString(); } Java Q1. ্هͷίʔυͷҙਤΛ౴͑Αɻ ʢճ౴࣌ؒ: 10 ඵɺ഑఺: 5 ఺ʣ ίϨΫγϣϯ
  53. public String func(List<Person> persons) { List<Person> filtered = new ArrayList<Person>();

    for (Person p : persons) { if (p.getAge() >= 18) { filtered.add(p); } } StringBuffer buf = new StringBuffer(); for (Person p : filtered) { buf.append(p.getName() + "," + p.getAge() + "\n"); } return buf.toString(); } Java Q1. ্هͷίʔυͷҙਤΛ౴͑Αɻ ʢճ౴࣌ؒ: 10 ඵɺ഑఺: 5 ఺ʣ 18 ࡀະຬͷਓΛϑΟϧλ ֤ Person Λ CSV ʹม׵ ίϨΫγϣϯ
  54. ΍Γ͍ͨࣄ: ίϨΫγϣϯૢ࡞ ݱ࣮: for จ & if จ஍ࠈ ίϨΫγϣϯ

  55. def func(persons: List[Person]): String = persons .filter(p => p.age >=

    18) .map(p => p.name + “,” + p.age) .mkString(“,”) Scala Scala ίϨΫγϣϯͳΒ ؆ܿ͞ˍಡΈ΍͕͢͞UPʂ ίϨΫγϣϯ ม׵ࡁΈϦετ͕໭Γ஋ͳͷͰ ࣍ʑͱ࿈࠯Ͱ͖Δ
  56. def func(persons: List[Person]): String = persons .filter(p => p.age >=

    18) .map(p => p.name + “,” + p.age) .mkString(“,”) Scala Scala ίϨΫγϣϯͳΒ ؆ܿ͞ˍಡΈ΍͕͢͞UPʂ ίϨΫγϣϯ ม׵ࡁΈϦετ͕໭Γ஋ͳͷͰ ࣍ʑͱ࿈࠯Ͱ͖Δ ͜Ε͸Կʁ
  57. persons.filter(p => p.age >= 18) Scala ίʔϧόοΫΛ؆ܿʹॻ͚Δ౶ҥߏจ ͱ͍͏ཧղͰͱΓ͋͑ͣແ໰୊ͩͱࢥ͍·͢ɻ ίϨΫγϣϯ persons.filter(new

    Function1[Person, Boolean] { def apply(p: Person): Boolean = p.age >= 18 }) ʹ Person Λೖྗͯ͠ Boolean Λग़ྗ͢Δ p ΛҰͭͣͭ apply ʹ౉ͯ͠൑ఆ
  58. ଞʹ΋ศརͳϝιου͕100Ҏ্ from http://www.ne.jp/asahi/hishidama/home/tech/scala/collection/method.html ίϨΫγϣϯ

  59. Scala Ͱ͸ɺ܁Γฦ͠ॲཧͰ for ࣜΛ࢖͏ػձ͸͋·Γ͋Γ·ͤΜ 1.େ఍ͷ܁Γฦ͠ॲཧ͸ίϨΫγϣϯԋࢉΛ࢖ͬͯॻ͚Δɻ 2.ԋࢉ໊͕υΩϡϝϯτʹͳΔͷͰɺҙਤ͕໌֬ʹͳΔɻ ίϨΫγϣϯ

  60. ՄมίϨΫγϣϯ ͱ ෆมίϨΫγϣϯ ίϨΫγϣϯ

  61. http://docs.scala-lang.org/ja/overviews/collections/overview.html ՄมίϨΫγϣϯ •ίϨΫγϣϯ಺ͷཁૉΛมߋͰ͖Δɻ •Java ൛ͷίϨΫγϣϯ͸جຊతʹՄมɻ ίϨΫγϣϯ

  62. http://docs.scala-lang.org/ja/overviews/collections/overview.html ෆมίϨΫγϣϯ •ίϨΫγϣϯ಺ͷཁૉΛมߋͰ͖ͳ͍ɻ •มߋ͍ͨ࣌͠͸৽͍͠ίϨΫγϣϯΛ࡞Δɻ ίϨΫγϣϯ

  63. ෆมίϨΫγϣϯ ͬͯ ͍Βͳ͍ࢠ…ʁ ίϨΫγϣϯ

  64. ҧ͏Αʂ ίϨΫγϣϯ

  65. ෆม ঢ়ଶΛ࣋ͨͳ͍ ͍ͭͰ΋ɺͲ͜Ͱ΋ ಉ݁͡Ռ͕ಘΒΕΔ ʹ ίϨΫγϣϯ ʹ

  66. ྫ: ςετ ଞͷϞδϡʔϧ΁౉ͨ͠ͱ͖ʹɺ ཁૉ͕มߋ͞Εͳ͍͜ͱΛอূͰ͖Δɻ Ϟδϡʔϧͷಠཱੑ͕ߴ·Γ ςετ͠΍͍͢ɻ ίϨΫγϣϯ

  67. ྫ: ฒߦॲཧ ଞͷεϨου΁౉ͨ͠ͱ͖ʹɺ ཁૉ͕มߋ͞Εͳ͍͜ͱΛอূͰ͖Δ ฒߦϓϩάϥϛϯάʹىҼ͢Δ ਺ʑͷόά͔Βղ์͞ΕΔ ίϨΫγϣϯ

  68. ෆมίϨΫγϣϯ Λ࢖͏ͷ͕ Scala ྲྀ ίϨΫγϣϯ ໌ࣔతʹ import ͠ͳ͍ͱෆม൛ʹͳΔ

  69. ͱ͸͍ͬͯ΋ ࠷ॳ͸ ՄมίϨΫγϣϯ Ͱແ໰୊ ίϨΫγϣϯ ׳ΕͨΒෆม൛ʹ ੾Γସ͍͑ͯ͜͏

  70. ฒྻίϨΫγϣϯ ίϨΫγϣϯ

  71. γϯάϧίΞੑೳ޲্ͷݶք ϑϦʔϥϯν͸ऴΘͬͨʂ (ŝžŕ ίϨΫγϣϯ

  72. ϚϧνίΞରԠ Ͱ ੑೳΛҾ͖ग़͍ͨ͠ ίϨΫγϣϯ

  73. val ret = (1 to 10000000).map(_ + 1) Scala ී௨ͷίϨΫγϣϯԋࢉ͸

    ॱ࣮࣍ߦ ίϨΫγϣϯ
  74. val ret = (1 to 10000000).par.map(_ + 1) Scala ී௨ͷίϨΫγϣϯΛ

    ؆୯ʹ ฒྻܭࢉରԠʹͰ͖Δʂ par Λݺͼग़͚ͩ͢ʂ ίϨΫγϣϯ
  75. ·ͱΊ • Scala ͷڧྗͳίϨΫγϣϯԋࢉ • ίʔυͷݟ௨͠Λେ෯ʹվળɻ • ෆมίϨΫγϣϯ…࢖ͬͯ΋͍͍ͷΑʁ • ؔ਺ܕϓϩάϥϛϯά΁ͷট଴ঢ়ɻ

    • ϚϧνίΞΛ׆༻͢ΔฒྻॲཧԽ΋؆୯ɻ ίϨΫγϣϯ
  76. Option ܕ Option ܕ

  77. Option ܕ •”ࣦഊ͢Δ͔΋͠Εͳ͍ܭࢉ”Λදݱ͢Δ • ඪ४ϥΠϒϥϦͷҰ෦ (scala.Option) Option ܕ

  78. I call it my billion-dollar mistake. It was the invention

    of the null reference in 1965. Tony Hoare (2009). “Null Reference: The Billion Dollar Mistake”. QCon London “null ͸ 10 ԯυϧͷࣦഊ” ※ʮΫΠοΫιʔτʯΛൃ໌ͨ͠ਓ Option ܕ
  79. ͋Γ͕ͪͳόά Person person = map.get(“tom”); println(person.getName()); Option ܕ map.get ͸

    null Λฦ͔͢΋… NullPointerException!
  80. null ͷ໰୊఺ •”஋͕ͳ͍ঢ়ଶ” ͱ ”ܭࢉͷࣦഊ” Λ۠ผͰ͖ͳ͍ɻ •ίϯύΠϥ͕ݕࠪͰ͖ͳ͍ͷͰɺϓϩάϥϚ͕๨Εͣ ʹ null νΣοΫΛ͠ͳ͚Ε͹ͳΒͳ͍ɻ

    Option ܕ
  81. sealed abstract class Option[+A] final case class Some[+A](x: A) extends

    Option[A] object None extends Option[Nothing] Option None -x: A Some ͦ͜Ͱ Option ܕ Option ܕ ”ࣦഊͨ͠ܭࢉ”ΛදݱͰ͖Δσʔλܕ case Ϋϥεʂ
  82. Option ܕΛ࢖͏ val person: Option[Person] = map.get(“tom”) person match {

    case Some(p) => println(p.name) case None => println(“no value!”) } ύλʔϯϚονͰ ҆શʹ஋ΛऔΓग़ͤΔ Option ܕ
  83. person match { case Some(p) => println p.name // If

    you forget ‘case None =>’... } warning: match may not be exhaustive. It would fail on the following input: None person match { ^ ίϯύΠϥ͕ ϛεΛݟ͚ͭͯܯࠂͯ͘͠ΕΔʂ Option ܕ ͏͔ͬΓ None Λ ๨Εͯ΋… ίϯύΠϥ: ʮNone ͷఆ͕ٛແ͘ͳ͍ʁʯ
  84. ίϨΫγϣϯԋࢉͱͷ૊Έ߹Θͤ person map { p => p.name // Someͷ͚࣌ͩ஋Λऔग़͠ }

    orElse { Some(“no value!”) // Noneͷ͚࣌ͩஔ͖׵͑Δ } foreach { name => println(name) // Someͷ͚࣌ͩग़ྗ } ώϯτ: Option ͸ཁૉ਺͕ 0 ͔ 1 ͷ List Option ܕ Ϧετͱಉ͡ϝιου…ʁ
  85. Optionͷ਌ੰ • ࣦഊͨ͠ཧ༝Λอ࣋Ͱ͖Δ • Either ܕ • Try ܕʢScala 2.10

    Ҏ߱ʣ • ࣦഊ͢Δ͔΋͠Εͳ͍ඇಉظܭࢉ • Future ܕʢScala 2.10 Ҏ߱ʣ Option ܕ
  86. ·ͱΊ • Option ܕ͸ ”ࣦഊ͢Δ͔΋͠Εͳ͍ܭࢉ” Λදݱ͢Δ • ύλʔϯϚον΍ίϨΫγϣϯԋࢉͱ૊Έ߹ΘͤΔ ͱɺܭࢉ݁ՌΛ҆શʹऔΓग़ͤΔɻ Option

    ܕ
  87. ʢ༨ஊʣ • Option ܕ͸ ”ܕͷྗ” Λੜ͔ͨ͠ϓϩάϥϛϯάͷ޷ྫ • จ຺Λ࣋ͬͨܕʢe.g. ܭࢉͷ੒ޭɾࣦഊʣ •

    ίϯύΠϥ͕ɺܕΛ࢖ͬͯίʔυͷ҆શੑΛݕࠪͯ͠ ͘ΕΔɻ Option ܕ
  88. શମͷ·ͱΊ • Scala ͸ ”Better Java”ɻ͜Θ͘ͳ͍Αʂ • “Better Java” ͚ͩ͡Όͳ͍

    • ”ܕͷྗ”Λ׆͔ͨ҆͠શͳϓϩάϥϛϯάɻ • ͦͯؔ͠਺ܕϓϩάϥϛϯά΁…ɻ