Slide 1

Slide 1 text

େ վ ଄ ʂ ੩ త Ϗ ϑ Υ ʔ ɾ Ξ ϑ λ ʔ d 4DBMBϦϑΝΫλϦϯάೖ໳ !UPEPLS . : ✨

Slide 2

Slide 2 text

#FUUFS+BWBͳίʔυ 4DBMBΒ͠͞Λ׆͔͠ɺΑΓݎ࿚ʹ

Slide 3

Slide 3 text

ా ॴ ॣ ༎ ! U P E P L S ג ࣜ ձ ࣾ Ϗ ζ Ϧ ʔ ν 
 ελ ϯόΠ ࣄ ۀ ෦ α ʔ ν ί Ξάϧ ʔϓ 
 ΢ Σ ϒΫ ϩ ʔ ϥ ʔ ͷ ։ ൃ ɾ ӡ ༻ ೥ ݄ ೖ ࣾ ࡀ 4 D B M B ྺ ೥ ϲ ݄

Slide 4

Slide 4 text

͓ ࿩ ͢͠ Δ ͜ ͱ w ͳͥ#FUUFS+BWBͷઌͳͷ͔ʁ4DBMBΒ͠͞ͷԸܙͱ͸ w ύλʔϯϚονͷύλʔϯΩϟετΛͳͯ҆͘͠શʹ w λϓϧΛආ͚Δͦͷ਺ࣈʹҙຯ͸͋Δ͔ʁ w 4DBMBͳྫ֎ॲཧ੒ޭɾࣦഊΛ஋ͱͯ͠දݱ͢Δ w DBTFDMBTTʹ੍໿Λ࣋ͨͤΔSFRVJSFͷར༻ w TFBMFEUSBJUͰ໢ཏੑΛ୲อ͢ΔίϯύΠϥʹ͓ئ͍

Slide 5

Slide 5 text

͓ ࿩ ͢͠ Δ ͜ ͱ w ͳͥ#FUUFS+BWBͷઌͳͷ͔ʁ4DBMBΒ͠͞ͷԸܙͱ͸ w ύλʔϯϚονͷύλʔϯΩϟετΛͳͯ҆͘͠શʹ w λϓϧΛආ͚Δͦͷ਺ࣈʹҙຯ͸͋Δ͔ʁ w 4DBMBͳྫ֎ॲཧ੒ޭɾࣦഊΛ஋ͱͯ͠දݱ͢Δ w DBTFDMBTTʹ੍໿Λ࣋ͨͤΔSFRVJSFͷར༻ w TFBMFEUSBJUͰ໢ཏੑΛ୲อ͢ΔίϯύΠϥʹ͓ئ͍ ˠ 4 D B M B Λ ֶ ͼ ࢝ Ί ͨ ࠒ ͷ ࣗ ෼ ʹ 
 ɹ ڭ ͑ͯ ͋ ͛ ͨ ͍ ಺ ༰

Slide 6

Slide 6 text

ͳ ͥ # F U U F S + B W B ͷ ઌ ͳ ͷ ͔ ʁ

Slide 7

Slide 7 text

1 def isBlank(str:String): Boolean = { 2 var strLen = 0 3 4 if(str == null || { strLen = str.length(); strLen } == 0) { 5 return true 6 } 7 8 var i = 0 9 while( i < strLen ){ 10 if(Character.isWhitespace( str.charAt(i) ) == false){ 11 return false 12 } 13 i += 1 14 } 15 16 return true 17 } # F U U F S + B W B ͳ ελ Πϧ

Slide 8

Slide 8 text

1 def isBlank(str:String): Boolean = { 2 var strLen = 0 3 4 if(str == null || { strLen = str.length(); strLen } == 0) { 5 return true 6 } 7 8 var i = 0 9 while( i < strLen ){ 10 if(Character.isWhitespace( str.charAt(i) ) == false){ 11 return false 12 } 13 i += 1 14 } 15 16 return true 17 } # F U U F S + B W B ͳ ελ Πϧ

Slide 9

Slide 9 text

1 def isBlank(str:String): Boolean = { 2 var strLen = 0 3 4 if(str == null || { strLen = str.length(); strLen } == 0) { 5 return true 6 } 7 8 var i = 0 9 while( i < strLen ){ 10 if(Character.isWhitespace( str.charAt(i) ) == false){ 11 return false 12 } 13 i += 1 14 } 15 16 return true 17 } # F U U F S + B W B ͳ ελ Πϧ ͪΐͬͱ௕͍

Slide 10

Slide 10 text

1 def isBlank(str:String): Boolean = { 2 var strLen = 0 3 4 if(str == null || { strLen = str.length(); strLen } == 0) { 5 return true 6 } 7 8 var i = 0 9 while( i < strLen ){ 10 if(Character.isWhitespace( str.charAt(i) ) == false){ 11 return false 12 } 13 i += 1 14 } 15 16 return true 17 } # F U U F S + B W B ͳ ελ Πϧ ίʔυͷ௕͞ͱόάͷೖΓ΍͢͞͸
 ਖ਼ൺྫ͢ΔɻͰ͖Ε͹΋ͬͱ୹͍ͨ͘͠ɻ ͪΐͬͱ௕͍

Slide 11

Slide 11 text

1 def isBlank(str:String): Boolean = { 2 var strLen = 0 3 4 if(str == null || { strLen = str.length(); strLen } == 0) { 5 return true 6 } 7 8 var i = 0 9 while( i < strLen ){ 10 if(Character.isWhitespace( str.charAt(i) ) == false){ 11 return false 12 } 13 i += 1 14 } 15 16 return true 17 } # F U U F S + B W B ͳ ελ Πϧ

Slide 12

Slide 12 text

1 def isBlank(str:String): Boolean = { 2 var strLen = 0 3 4 if(str == null || { strLen = str.length(); strLen } == 0) { 5 return true 6 } 7 8 var i = 0 9 while( i < strLen ){ 10 if(Character.isWhitespace( str.charAt(i) ) == false){ 11 return false 12 } 13 i += 1 14 } 15 16 return true 17 } # F U U F S + B W B ͳ ελ Πϧ ग़ޱ͕Օॴ

Slide 13

Slide 13 text

1 def isBlank(str:String): Boolean = { 2 var strLen = 0 3 4 if(str == null || { strLen = str.length(); strLen } == 0) { 5 return true 6 } 7 8 var i = 0 9 while( i < strLen ){ 10 if(Character.isWhitespace( str.charAt(i) ) == false){ 11 return false 12 } 13 i += 1 14 } 15 16 return true 17 } # F U U F S + B W B ͳ ελ Πϧ ग़ޱ͕Օॴ Ͱ͖Ε͹෼ذ͸গͳ͍΄͏͕
 γϯϓϧͰݎ࿚ɻ

Slide 14

Slide 14 text

1 def isBlank(str:String): Boolean = { 2 var strLen = 0 3 4 if(str == null || { strLen = str.length(); strLen } == 0) { 5 return true 6 } 7 8 var i = 0 9 while( i < strLen ){ 10 if(Character.isWhitespace( str.charAt(i) ) == false){ 11 return false 12 } 13 i += 1 14 } 15 16 return true 17 } # F U U F S + B W B ͳ ελ Πϧ

Slide 15

Slide 15 text

1 def isBlank(str:String): Boolean = { 2 var strLen = 0 3 4 if(str == null || { strLen = str.length(); strLen } == 0) { 5 return true 6 } 7 8 var i = 0 9 while( i < strLen ){ 10 if(Character.isWhitespace( str.charAt(i) ) == false){ 11 return false 12 } 13 i += 1 14 } 15 16 return true 17 } # F U U F S + B W B ͳ ελ Πϧ ࠶୅ೖՄೳͳม਺

Slide 16

Slide 16 text

1 def isBlank(str:String): Boolean = { 2 var strLen = 0 3 4 if(str == null || { strLen = str.length(); strLen } == 0) { 5 return true 6 } 7 8 var i = 0 9 while( i < strLen ){ 10 if(Character.isWhitespace( str.charAt(i) ) == false){ 11 return false 12 } 13 i += 1 14 } 15 16 return true 17 } # F U U F S + B W B ͳ ελ Πϧ ࠶୅ೖՄೳͳม਺ ࠶୅ೖՄೳʮঢ়ଶʯΛ࣋ͭͱ͍͏͜ͱɻ
 ঢ়ଶΛؾʹ͢Δඞཁ͕͋Δɻ

Slide 17

Slide 17 text

1 def isBlank(str: String): Boolean = 2 Option(str).fold(true)(_.forall(Character.isWhitespace)) 4 D B M B Β ͠ ͍ ελ Πϧ

Slide 18

Slide 18 text

1 def isBlank(str: String): Boolean = 2 Option(str).fold(true)(_.forall(Character.isWhitespace)) w ୹͘ಡΈ΍͘͢ w ෼ذ͕গͳ͘ w ঢ়ଶΛؾʹ͢Δඞཁͳ͘ 4 D B M B Β ͠ ͍ ελ Πϧ

Slide 19

Slide 19 text

1 def isBlank(str: String): Boolean = 2 Option(str).fold(true)(_.forall(Character.isWhitespace)) w ୹͘ಡΈ΍͘͢ w ෼ذ͕গͳ͘ w ঢ়ଶΛؾʹ͢Δඞཁͳ͘ 4 D B M B Β ͠ ͍ ελ Πϧ ☺ όάͷࠞೖͷՄೳੑ͕Լ͕Δ

Slide 20

Slide 20 text

ύ λ ʔϯ Ϛ ο ν ͷ ύ λ ʔϯ

Slide 21

Slide 21 text

0 Q U J P O ͷ ύ λ ʔϯ Ϛ ο ν 1 option match { 2 case Some(x) => println(s"$x is here!") 3 case None => println("oh…") 4 }

Slide 22

Slide 22 text

γ ʔ έ ϯε ͷ ύ λ ʔϯ Ϛ ο ν 1 List("Noel", "Liam", "Gem", "Andy", "Alan") match { 2 case List(_, second, _*) => println(s"Hello $second!") 3 case _ => 4 } 5 // => Hello Liam!

Slide 23

Slide 23 text

ί ϯε τ ϥ Ϋ λύ λ ʔϯ 1 val user = User("Martin", Place("Tokyo", "Shibuya")) 2 3 user match { 4 case User(name, Place(pref, _)) => "$name in $pref" 5 case _ => 6 } 7 // => Martin lives in Tokyo!

Slide 24

Slide 24 text

ܕ ν Σ ο Ϋ ͱ Ω ϟε τ 1 def count2x(x: Any): Int = { 2 if (x.isInstanceOf[String]) { 3 x.asInstanceOf[String].length * 2 4 } else if (x.isInstanceOf[Int]) { 5 x.asInstanceOf[Int] * 2 6 } else -1 7 } 8 9 count2x("scala") // 10 10 count2x(7) // 14

Slide 25

Slide 25 text

ܕ ν Σ ο Ϋ ͱ Ω ϟε τ 1 def count2x(x: Any): Int = { 2 if (x.isInstanceOf[String]) { 3 x.asInstanceOf[String].length * 2 4 } else if (x.isInstanceOf[Int]) { 5 x.asInstanceOf[Int] * 2 6 } else -1 7 } 8 9 count2x("scala") // 10 10 count2x(7) // 14 ܕνΣοΫ

Slide 26

Slide 26 text

ܕ ν Σ ο Ϋ ͱ Ω ϟε τ 1 def count2x(x: Any): Int = { 2 if (x.isInstanceOf[String]) { 3 x.asInstanceOf[String].length * 2 4 } else if (x.isInstanceOf[Int]) { 5 x.asInstanceOf[Int] * 2 6 } else -1 7 } 8 9 count2x("scala") // 10 10 count2x(7) // 14 Ωϟετ

Slide 27

Slide 27 text

ܕ ν Σ ο Ϋ ͱ Ω ϟε τ 1 def count2x(x: Any): Int = { 2 if (x.isInstanceOf[String]) { 3 x.asInstanceOf[String].length * 2 4 } else if (x.isInstanceOf[Int]) { 5 x.asInstanceOf[Int] * 2 6 } else -1 7 } 8 9 count2x("scala") // 10 10 count2x(7) // 14 Ωϟετ গ͠৑௕ɻ Θ͟Θ͟Ωϟετ͢Δͷ΋ةݥͳ߳Γɻ ܕνΣοΫ

Slide 28

Slide 28 text

ܕ ෇ ͖ ύ λ ʔϯ 1 def count2x(x: Any): Int = x match { 2 case x:String => x.length * 2 3 case x: Int => x * 2 4 case _ => -1 5 } 6 7 count2x("scala") // 10 8 count2x(7) // 14

Slide 29

Slide 29 text

ܕ ෇ ͖ ύ λ ʔϯ 1 def count2x(x: Any): Int = x match { 2 case x:String => x.length * 2 3 case x: Int => x * 2 4 case _ => -1 5 } 6 7 count2x("scala") // 10 8 count2x(7) // 14 ☺ ίʔυͷݟ௨͕͠޲্ɻ
 Ωϟετ΋ෆཁͰ҆৺ɻ

Slide 30

Slide 30 text

ܕ ফ ڈ ʹ ஫ ҙ 1 def detectType(x: Any): String = x match { 2 case x: List[Int] => "This is List of Int!" 3 case _ => "What?" 4 } 5 6 detectType(List("string!")) // => This is List of Int!

Slide 31

Slide 31 text

ܕ ফ ڈ ʹ ஫ ҙ 1 def detectType(x: Any): String = x match { 2 case x: List[Int] => "This is List of Int!" 3 case _ => "What?" 4 } 5 6 detectType(List("string!")) // => This is List of Int! +BWBͱಉ༷ɺίϨΫγϣϯͷཁૉܕͷ৘ใ͸
 ࣮ߦ࣌ʹ͸؅ཧ͞Εͳ͍ ˞"SSBZΛআ͘

Slide 32

Slide 32 text

λ ϓϧ Λ ආ ͚ Δ

Slide 33

Slide 33 text

λ ϓϧ 1 // ঎඼ 2 case class Item(name: String, price: Int) 3 4 // ঎඼ͷ੫ࠐՁ֨ͱՁ֨ϥϕϧΛฦ͢ 5 def labelPriceWithTax(item: Item): (Int, String) = { 6 val withTax = item.price * 1.08 7 val label = s"ܹ҆େಛՁ ¥${item.price}ʢ੫ࠐʣ" 8 (withTax, label) 9 }

Slide 34

Slide 34 text

λ ϓϧ 1 // ঎඼ 2 case class Item(name: String, price: Int) 3 4 // ঎඼ͷ੫ࠐՁ֨ͱՁ֨ϥϕϧΛฦ͢ 5 def labelPriceWithTax(item: Item): (Int, String) = { 6 val withTax = item.price * 1.08 7 val label = s"ܹ҆େಛՁ ¥${item.price}ʢ੫ࠐʣ" 8 (withTax, label) 9 } ☺ +BWBͰ͸-JTU0CKFDUͷΑ͏ʹฦ͍ͯͨ͠΋ͷ͕
 ܕͷ৘ใΛΩʔϓͨ͠··ฦͤͯ҆৺

Slide 35

Slide 35 text

Ξ ϯ νύ λ ʔϯ Ξ ϯ μʔε ί Ξ Ͱ ͷ ΞΫ η ε 1 val apple = Item("αϯ;͡", 158) 2 val x = labelPriceWithTax(apple) 3 println(s"${x._1}͕ͳΜͱ${x._2}!") 4 // αϯ;͕͡ͳΜͱܹ҆େಛՁ ¥158!

Slide 36

Slide 36 text

Ξ ϯ νύ λ ʔϯ Ξ ϯ μʔε ί Ξ Ͱ ͷ ΞΫ η ε 1 val apple = Item("αϯ;͡", 158) 2 val x = labelPriceWithTax(apple) 3 println(s"${x._1}͕ͳΜͱ${x._2}!") 4 // αϯ;͕͡ͳΜͱܹ҆େಛՁ ¥158! ʮY@ʯͷͳͲͷ਺ࣈ͸
 ཁૉʹ͍ͭͯͷ৘ใΛԿ΋఻͍͑ͯͳ͍ɻ
 औΓҧ͑ͯ΋ؾ͖ͮͮΒ͍ɻ

Slide 37

Slide 37 text

վ ળ ྫ ม ਺ ୅ ೖ ࣌ ʹ λ ϓϧ Λ ෼ ղ 1 val apple = Item("αϯ;͡", 158) 2 val (name, priceLabel) = labelPriceWithTax(apple) 3 println(s"${name}͕ͳΜͱ${priceLabel}!") 4 // αϯ;͕͡ͳΜͱܹ҆େಛՁ ¥158! ҙຯͷ͋Δม਺໊ͰΞΫηεͰ͖ͯ҆৺
 ม਺͕૿͑ͯ΋औΓҧ͑ʹ͍͘ ☺

Slide 38

Slide 38 text

վ ળ ྫ ύ λ ʔϯ Ϛ ο ν Ͱ λ ϓϧ Λ ෼ ղ 1 val apple = Item("αϯ;͡", 158) 2 labelPriceWithTax(apple) match { case (name, price) => 3 println(s"${name}͕ͳΜͱ${price}!") 4 } 5 // αϯ;͕͡ͳΜͱܹ҆େಛՁ ¥158!

Slide 39

Slide 39 text

4 D B M B ͳ ྫ ֎ ॲ ཧ

Slide 40

Slide 40 text

ྫ ֎ ॲ ཧ 1 // ྫ֎Λ౤͛ΔՄೳੑͷ͋Δϝιου 2 def detect(bytes: Array[Byte]): Charset = { 3 val detectedCharsetName: String = ... 4 Charset.forName(detectedCharsetName) 5 } 6 7 // ϝιουͷݺͼग़͠ݩͰྫ֎ॲཧ 8 try { 9 detect(bytes) 10 ... 11 } catch { 12 case e: Exception => ... 13 }

Slide 41

Slide 41 text

ྫ ֎ ॲ ཧ 1 // ྫ֎Λ౤͛ΔՄೳੑͷ͋Δϝιου 2 def detect(bytes: Array[Byte]): Charset = { 3 val detectedCharsetName: String = ... 4 Charset.forName(detectedCharsetName) 5 } 6 7 // ϝιουͷݺͼग़͠ݩͰྫ֎ॲཧ 8 try { 9 detect(bytes) 10 ... 11 } catch { 12 case e: Exception => ... 13 } ੒ޭPSࣦഊΛ஋ͱͯ͠ѻ͍ɺ
 ੍ޚϑϩʔΛγϯϓϧʹ͍ͨ͠

Slide 42

Slide 42 text

ྫ ֎ Ͱ ͸ ͳ ͘ ʜ ੒ޭPSࣦഊΛදݱ͢Δܕͱͦͷ஋Λ༻͍Δ w 0QUJPO<5>
 ੒ޭ࣌ͷ஋Λ4PNFɺࣦഊΛ/POFͰදݱ w &JUIFS<" #>
 ੒ޭ࣌ͷ৘ใΛ3JHIUͰɺࣦഊͷ৘ใΛ-FGUͰදݱ w 5SZ<5>
 ྫ֎Λ౤͛ΔՄೳੑͷ͋ΔॲཧΛந৅Խɺ4VDDFTTͱ'BJMVSFͰ݁ՌΛදݱ

Slide 43

Slide 43 text

0 Q U J P O ܕ w 4PNFͱ/POFͱ͍͏छྨ w ஋͕ଘࡏ͢Δ͜ͱΛද͢4PNF W w ஋͕ଘࡏ͠ͳ͍͜ͱΛද͢/POF w 0QUJPOܕˠʮ஋͕ଘࡏ͠ͳ͍Մೳੑ͕͋Δʯͱ͍͏ϝοηʔδ

Slide 44

Slide 44 text

0 Q U J P O Ͱ ੒ ޭ ࣦ ഊ Λ ද ݱ ͢ Δ 1 // try / catchͰOptionΛฦ͢ 2 def detect(bytes: Array[Byte]): Option[Charset] = { 3 val detectedCharsetName: String = ... 4 try { 5 Some(Charset.forName(detectedCharsetName)) 6 } catch { 7 case e: Exception => None 8 } 9 } ੒ޭ࣌͸4PNFɺࣦഊ࣌͸/POFͰฦ͢

Slide 45

Slide 45 text

0 Q U J P O Ͱ ੒ ޭ ࣦ ഊ Λ ද ݱ ͢ Δ 1 // ࣦഊͨ͠ΒσϑΥϧτ஋Λ࢖͏ 2 detect(bytes).getOrElse(Charset.defaultCharset) 3 4 // ࣦഊͨ͠ΒԿ΋͠ͳ͍ 5 detect(bytes).map { charset => 6 // Կ͔ॲཧ 7 } ݺͿଆͰͷѻ͍ ݁ՌʹԠͨ͡ॲཧ͕҆શ͔ͭॊೈʹͰ͖Δ ☺

Slide 46

Slide 46 text

& J U I F S ܕ w 3JHIU D ͱ-FGU F ͱ͍͏छྨ w 3JHIU͸ʮਖ਼͍͠ʯʹ͔͚ͯ੒ޭ࣌ͷ஋Λ֨ೲ͢Δͷ͕ओྲྀ w -FGU΋஋Λ࣋ͯΔͷͰɺࣦഊ࣌ͷ৘ใΛ֨ೲͰ͖Δ w &JUIFSܕˠʮࣦഊ͢ΔՄೳੑ͕͋Δʯͱ͍͏ϝοηʔδ

Slide 47

Slide 47 text

& J U I F S Ͱ ੒ ޭ ࣦ ഊ Λ ද ݱ ͢ Δ 1 // try / catchͰOptionΛฦ͢ 2 def detect(bytes: Array[Byte]): Either[Exception, Charset] = { 3 val detectedCharsetName: String = ... 4 try { 5 Right(Charset.forName(detectedCharsetName)) 6 } catch { 7 case e: Exception => Left(e) 8 } 9 } ੒ޭ࣌͸3JHIUɺࣦഊ࣌͸-FGUͰฦ͢

Slide 48

Slide 48 text

& J U I F S Ͱ ੒ ޭ ࣦ ഊ Λ ද ݱ ͢ Δ 1 // 2 val charset = detect(bytes) match { 3 case Right(charset) => charset 4 case Left(e) => 5 logger.error(s"Oops! :$e") 6 Charset.defaultCharset 7 } ݺͿଆͰͷѻ͍ ݺͼग़͢ਓʹ݁ՌͷνΣοΫΛڧ੍Ͱ͖Δ
 ࣦഊ࣌ͷ৘ใ͕࣋ͯΔͷͰϩάʹग़ͨ͠ΓͳͲ ☺

Slide 49

Slide 49 text

5S Z ܕ w 4VDDFTT W ͱ'BJMVSF F ͱ͍͏छྨ w 4VDDFTT W ʹ੒ޭ࣌ͷ஋ w 'BJMVSF F ʹࣦഊ࣌ͷ৘ใ w ྫ֎͕౤͛ΒΕΔ͔΋͠Εͳ͍ॲཧΛ5SZͰғΉ͚ͩͰ0, w 5SZܕˠʮࣦഊ͢ΔՄೳੑ͕͋Δʯͱ͍͏ϝοηʔδ

Slide 50

Slide 50 text

5S Z Ͱ ੒ ޭ ࣦ ഊ Λ ද ݱ ͢ Δ 1 // ݁ՌΛTryͰड͚औΔ 2 import scala.util.Try 3 def detect(bytes: Array[Byte]): Try[Charset] = { 4 val detectedCharsetName: String = ... 5 Try(Charset.forName(ud.getDetectedCharset)) 6 } 7 ࣦഊ͢Δ͔΋͠Εͳ͍ॲཧΛ5SZ ͰแΉ

Slide 51

Slide 51 text

5S Z Ͱ ੒ ޭ ࣦ ഊ Λ ද ݱ ͢ Δ 1 // 2 val charset = detect(bytes) match { 3 case Success(charset) => charset 4 case Failure(e) => 5 logger.error(s"Oops! :$e") 6 Charset.defaultCharset 7 } ݺͿଆͰͷѻ͍ 4VDDFTT'BJMVSFͰύλʔϯϚον ☺

Slide 52

Slide 52 text

' B J M V S F ʹ ͳ Δ ͷ ͸ / P O ' B U B M ͳ ΋ ͷ • VirtualMachineError • ThreadDeath • InterruptedException • LinkageError • ControlThrowable 'BUBMͳΤϥʔɾྫ֎͸ͦͷ··εϩʔ͞ΕΔ

Slide 53

Slide 53 text

5S Z ͱ G P S ࣜ 1 // ྫ֎Λى͔͜͢΋͠Εͳ͍ϝιου 2 def someMethod: Int = ??? 3 def anotherMethod: Int = ???

Slide 54

Slide 54 text

5S Z ͱ G P S ࣜ 1 // TryΛforࣜͰ߹੒͢Δ 2 val result = for { 3 result1 <- Try(someMethod) 4 result2 <- Try(anotherMethod) 5 } yield result1 + result2 6 7 result match { 8 case Success(result) => println(result) 9 case Failure(e) => println(s"Opps: ", e) 10 }

Slide 55

Slide 55 text

5S Z ͱ G P S ࣜ 1 // TryΛforࣜͰ߹੒͢Δ 2 val result = for { 3 result1 <- Try(someMethod) 4 result2 <- Try(anotherMethod) 5 } yield result1 + result2 6 7 result match { 8 case Success(result) => println(result) 9 case Failure(e) => println(s"Opps: ", e) 10 } ʮ"ͱ#͕྆ํ੒ޭͨ͠ͱ͖ͷΈ9͢Δʯ
 ͳͲ͕෼͔Γ΍͘͢දݱͰ͖Δ ☺

Slide 56

Slide 56 text

D B T F D M B T T ʹ ੍ ໿ Λ ࣋ ͨ ͤ Δ

Slide 57

Slide 57 text

Ұ ݟ ͳ Μ ͷ ม ఩ ΋ ͳ ͍ D B T F D M B T T 1 case class Person( 2 name: String, 3 age: Int 4 )

Slide 58

Slide 58 text

Ұ ݟ ͳ Μ ͷ ม ఩ ΋ ͳ ͍ D B T F D M B T T 1 case class Person( 2 name: String, 3 age: Int 4 ) ࡀͱ͔͸ͳ͍ΑͶɻ
 Ҿ਺ΛνΣοΫ͠ͳ͖Όɻ

Slide 59

Slide 59 text

1 case class Person( 2 name: String, 3 age: Int 4 ) { 5 if(age < 0) throw new IllegalArgumentException("Age must be natural!") 6 } ੍ ໿ Λ ΋ ͨ ͤ ͨ D B T F D M B T T

Slide 60

Slide 60 text

1 case class Person( 2 name: String, 3 age: Int 4 ) { 5 if(age < 0) throw new IllegalArgumentException("Age must be natural!") 6 } ੍ ໿ Λ ΋ ͨ ͤ ͨ D B T F D M B T T ࣄલ৚݅Ͱ͋Δ͜ͱΛ
 ΋ͬͱ໌֬ʹ఻͍͑ͨ

Slide 61

Slide 61 text

1 case class Person( 2 name: String, 3 age: Int 4 ) { 5 require(age >= 0, "Age must be natural number!") 6 } ੍ ໿ Λ ΋ ͨ ͤ ͨ D B T F D M B T T

Slide 62

Slide 62 text

1 case class Person( 2 name: String, 3 age: Int 4 ) { 5 require(age >= 0, "Age must be natural number!") 6 } ੍ ໿ Λ ΋ ͨ ͤ ͨ D B T F D M B T T ࣄલ৚݅

Slide 63

Slide 63 text

1 case class Person( 2 name: String, 3 age: Int 4 ) { 5 require(age >= 0, "Age must be natural number!") 6 } ੍ ໿ Λ ΋ ͨ ͤ ͨ D B T F D M B T T ࣄલ৚݅Λຬͨ͞ͳ͍৔߹ͷ
 Τϥʔϝοηʔδ

Slide 64

Slide 64 text

1 case class Person( 2 name: String, 3 age: Int 4 ) { 5 require(age >= 0, "Age must be natural number!") 6 } ੍ ໿ Λ ΋ ͨ ͤ ͨ D B T F D M B T T 1 val joe = Person("Joe Cool", -2))

Slide 65

Slide 65 text

1 case class Person( 2 name: String, 3 age: Int 4 ) { 5 require(age >= 0, "Age must be natural number!") 6 } ੍ ໿ Λ ΋ ͨ ͤ ͨ D B T F D M B T T 1 val joe = Person("Joe Cool", -2)) 2 // => java.lang.IllegalArgumentException: requirement failed: Age must be natural number!

Slide 66

Slide 66 text

1 case class Person( 2 name: String, 3 age: Int 4 ) { 5 require(age >= 0, "Age must be natural number!") 6 } ੍ ໿ Λ ΋ ͨ ͤ ͨ D B T F D M B T T &JUIFS΍5SZͱ૊Έ߹ΘͤΔͱ͏Ε͍͠ ☺ 1 val joe = Person("Joe Cool", -2)) 2 // => java.lang.IllegalArgumentException: requirement failed: Age must be natural number!

Slide 67

Slide 67 text

T F B M F E U S B J U Ͱ ໢ ཏ ੑ Λ ୲ อ ͢ Δ

Slide 68

Slide 68 text

Ϣ ʔ β ʔϓ ϥ ϯ Λ ύ λ ʔϯ Ϛ ο ν 1 case class User(name: String, age: Int)

Slide 69

Slide 69 text

Ϣ ʔ β ʔϓ ϥ ϯ Λ ύ λ ʔϯ Ϛ ο ν 1 case class User(name: String, age: Int) 2 3 trait Plan 4 case class Free(user: User) extends Plan // ແྉϓϥϯ 5 case class Premium(user: User) extends Plan // ϓϨϛΞϜϓϥϯ

Slide 70

Slide 70 text

Ϣ ʔ β ʔϓ ϥ ϯ Λ ύ λ ʔϯ Ϛ ο ν 1 case class User(name: String, age: Int) 2 3 trait Plan 4 case class Free(user: User) extends Plan // ແྉϓϥϯ 5 case class Premium(user: User) extends Plan // ϓϨϛΞϜϓϥϯ 6 7 val userA = Free(User("Taro", 23)) 8 val userB = Premium(User("Jiro", 25)) 9

Slide 71

Slide 71 text

Ϣ ʔ β ʔϓ ϥ ϯ Λ ύ λ ʔϯ Ϛ ο ν 1 case class User(name: String, age: Int) 2 3 trait Plan 4 case class Free(user: User) extends Plan // ແྉϓϥϯ 5 case class Premium(user: User) extends Plan // ϓϨϛΞϜϓϥϯ 6 7 val userA = Free(User("Taro", 23)) 8 val userB = Premium(User("Jiro", 25)) 9 10 userB match { 11 case Free(User(name, _)) => println(s"Hi, $name.") 12 case Premium(User(name, _)) => println(s"Hello, $name!") 13 }

Slide 72

Slide 72 text

Ϣ ʔ β ʔϓ ϥ ϯ Λ ύ λ ʔϯ Ϛ ο ν 1 case class User(name: String, age: Int) 2 3 trait Plan 4 case class Free(user: User) extends Plan // ແྉϓϥϯ 5 case class Premium(user: User) extends Plan // ϓϨϛΞϜϓϥϯ 6 7 val userA = Free(User("Taro", 23)) 8 val userB = Premium(User("Jiro", 25)) 9 10 userB match { 11 case Free(User(name, _)) => println(s"Hi, $name.") 12 case Premium(User(name, _)) => println(s"Hello, $name!") 13 } 14 // => Hello, Jiro!

Slide 73

Slide 73 text

ϓ ϥ ϯ Λ ૿ ΍ ͢ 1 case class User(name: String, age: Int) 2 3 trait Plan 4 case class Free(user: User) extends Plan 5 case class Premium(user: User) extends Plan

Slide 74

Slide 74 text

ϓ ϥ ϯ Λ ૿ ΍ ͢ 1 case class User(name: String, age: Int) 2 3 trait Plan 4 case class Free(user: User) extends Plan 5 case class Premium(user: User) extends Plan 6 case class Campaign(user: User) extends Plan 7 8 val userA = Free(User("Taro", 23)) 9 val userB = Premium(User("Jiro", 25)) 10 val userC = Campaign(User("Saburo", 28))

Slide 75

Slide 75 text

ϓ ϥ ϯ Λ ૿ ΍ ͢ 1 case class User(name: String, age: Int) 2 3 trait Plan 4 case class Free(user: User) extends Plan 5 case class Premium(user: User) extends Plan 6 case class Campaign(user: User) extends Plan 7 8 val userA = Free(User("Taro", 23)) 9 val userB = Premium(User("Jiro", 25)) 10 val userC = Campaign(User("Saburo", 28)) 11 12 Seq(userA, userB, userC) foreach { 13 case Free(User(name, _)) => println(s"Hi, $name.") 14 case Premium(User(name, _)) => println(s"Hello, $name!") 15 }

Slide 76

Slide 76 text

ϓ ϥ ϯ Λ ૿ ΍ ͢ 1 case class User(name: String, age: Int) 2 3 trait Plan 4 case class Free(user: User) extends Plan 5 case class Premium(user: User) extends Plan 6 case class Campaign(user: User) extends Plan 7 8 val userA = Free(User("Taro", 23)) 9 val userB = Premium(User("Jiro", 25)) 10 val userC = Campaign(User("Saburo", 28)) 11 12 Seq(userA, userB, userC) foreach { 13 case Free(User(name, _)) => println(s"Hi, $name.") 14 case Premium(User(name, _)) => println(s"Hello, $name!") 15 } 16 // => Hi, Taro. 17 // => Hello, Jiro! 18 // => scala.MatchError: Campaign(User(Saburo,28)) …

Slide 77

Slide 77 text

ϓ ϥ ϯ Λ ૿ ΍ ͢ 1 case class User(name: String, age: Int) 2 3 trait Plan 4 case class Free(user: User) extends Plan 5 case class Premium(user: User) extends Plan 6 case class Campaign(user: User) extends Plan 7 8 val userA = Free(User("Taro", 23)) 9 val userB = Premium(User("Jiro", 25)) 10 val userC = Campaign(User("Saburo", 28)) 11 12 Seq(userA, userB, userC) foreach { 13 case Free(User(name, _)) => println(s"Hi, $name.") 14 case Premium(User(name, _)) => println(s"Hello, $name!") 15 } 16 // => Hi, Taro. 17 // => Hello, Jiro! 18 // => scala.MatchError: Campaign(User(Saburo,28)) … .BUDI&SSPS͸࣮ߦ࣌Τϥʔɻ
 ࣮ߦલʹύλʔϯͷ࿙Εʹؾ͖͍ͮͨ

Slide 78

Slide 78 text

4 F B M F E म ০ ࢠ Ͱ ໢ ཏ ੑ Λ ୲ อ ͢ Δ 1 case class User(name: String, age: Int) 2 3 trait Plan

Slide 79

Slide 79 text

4 F B M F E म ০ ࢠ Ͱ ໢ ཏ ੑ Λ ୲ อ ͢ Δ 1 case class User(name: String, age: Int) 2 3 sealed trait Plan

Slide 80

Slide 80 text

4 F B M F E म ০ ࢠ Ͱ ໢ ཏ ੑ Λ ୲ อ ͢ Δ 1 case class User(name: String, age: Int) 2 3 sealed trait Plan w ಉ͡ϑΝΠϧ಺Ͱ͸ܧঝՄೳ w ผͷϑΝΠϧ͔Β͸ܧঝෆՄ

Slide 81

Slide 81 text

4 F B M F E म ০ ࢠ Ͱ ໢ ཏ ੑ Λ ୲ อ ͢ Δ 1 case class User(name: String, age: Int) 2 3 sealed trait Plan w ಉ͡ϑΝΠϧ಺Ͱ͸ܧঝՄೳ w ผͷϑΝΠϧ͔Β͸ܧঝෆՄ ͜ͷϑΝΠϧ಺Ͱఆٛ͞Εͨ΋ͷҎ֎ʹ
 1MBO͸ଘࡏ͠ͳ͍͜ͱ͕อূ͞ΕΔ

Slide 82

Slide 82 text

4 F B M F E म ০ ࢠ Ͱ ໢ ཏ ੑ Λ ୲ อ ͢ Δ 1 case class User(name: String, age: Int) 2 3 sealed trait Plan 4 case class Free(user: User) extends Plan 5 case class Premium(user: User) extends Plan 6 case class Campaign(user: User) extends Plan 7 8 val userA = Free(User("Taro", 23)) 9 val userB = Premium(User("Jiro", 25)) 10 val userC = Campaign(User("Saburo", 28)) 11 12 Seq(userA, userB, userC) foreach { 13 case Free(User(name, _)) => println(s"Hi, $name.") 14 case Premium(User(name, _)) => println(s"Hello, $name!") 15 }

Slide 83

Slide 83 text

[warn] It would fail on the following input: Campaign(_) [warn] Seq(userA, userB, userC) foreach { [warn] ^ [warn] one warning found 4 F B M F E म ০ ࢠ Ͱ ໢ ཏ ੑ Λ ୲ อ ͢ Δ ίϯύΠϧ͢Δͱʜ ʮ$BNQBJHO͕དྷͨΒམͪΔ΍Ͱʂʯͱ ίϯύΠϥ͕ڭ͑ͯ͘ΕΔ ☺

Slide 84

Slide 84 text

[warn] It would fail on the following input: Campaign(_) [warn] Seq(userA, userB, userC) foreach { [warn] ^ [warn] one warning found 4 F B M F E म ০ ࢠ Ͱ ໢ ཏ ੑ Λ ୲ อ ͢ Δ ίϯύΠϧ͢Δͱʜ ʮ$BNQBJHO͕དྷͨΒམͪΔ΍Ͱʂʯͱ ίϯύΠϥ͕ڭ͑ͯ͘ΕΔ ☺ 8BSOJOHແࢹ͠ͳ͍ͰͶ ☺

Slide 85

Slide 85 text

͓ ࿩ ͠ ͠ ͨ ͜ ͱ w ͳͥ#FUUFS+BWBͷઌͳͷ͔ʁ w ύλʔϯϚονͷύλʔϯ w λϓϧΛආ͚Δ w 4DBMBͳྫ֎ॲཧ w DBTFDMBTTʹ੍໿Λ࣋ͨͤΔ w TFBMFEUSBJUͰ໢ཏੑΛ୲อ͢Δ

Slide 86

Slide 86 text

) B W F B ( P P E 4 D B M B - J G F . : ✨