Share
Embed
Transcript
57 slides
-
͓͠ͳ͕͖ 1. ࣗݾհ 2. scala.Either ֓ཁ 3. scala.Either ͷෆศ 4.
ෆศͷݪҼ 5. scalaz.\/ -
ࣗݾհ ୩ޱ ྗే @tanishiking id: tanishiking24 ɾژେֶͰֶੜͬͯ·͢ ɾ࠷ۙͯͳΞϧόΠτ ɾڵຯͱͯ͠ ɹɹɾ
MySQL ɹɹɾ Python / Scala / shell script Scalaྺ: 6ϲ݄͘Β͍? -
ઌʹ·ͱΊ ɾscala.Either ʹෆࣗવͳڍಈ͕͍͔ͭ͋͘Δ ɾͦͷҰͭͷղܾࡦͱͯ͠ scalaz.\/ Λ͏ͱ͍͏બࢶ͕ ͋Δ ɾ؆୯ͳͱ͜Ζ(\/)͔Β scalaz ͍ͬͯ͘ͷྑͦ͞͏
ɾศརͳͱ͜Ζ͚͓ͩ͏ʂ ɾେࣄͳͷ scala.Either ͷڍಈΛ͓ͬͯ͘͜ͱʂ -
͓͠ͳ͕͖ 1. ࣗݾհ 2. scala.Either ֓ཁ 3. scala.Either ͷෆศ 4.
ෆศͷݪҼ 5. scalaz.\/ -
Either ɾEitherநΫϥεͰɺͦͷαϒΫϥεͱͯ͠Left,RightΛ࣋ͬͯΔ ɾRightʹਖ਼ৗɺLeftʹࣦഊͨ࣌͠ͷҟৗ sealed abstract class Either[+A, +B] extends Product
with Serializable final case class Left[+A, +B](a: A) extends Either[A, B] final case class Right[+A, +B](b: B) extends Either[A, B] https://github.com/scala/scala/blob/d6f66ec0f38e42c19f79cbe9d32d29c65dee1e05/src/library/scala/util/ Either.scala#L69 https://github.com/scala/scala/blob/d6f66ec0f38e42c19f79cbe9d32d29c65dee1e05/src/library/scala/util/ Either.scala#L182-L202 -
Either def parseInteger(numStr: String): Either[NumberFormatException, Int] = { try {
Right(numStr.toInt) } catch { case e: NumberFormatException => Left(e) } } ྫ֎Λ&JUIFSͰϥοϓͯ͠ΈͨΓ -
Either ɾEitherຊମʹ map / flatMap ͱ͍ͬͨϝιου ੜ͑ͯͳ͍ʂʂʂ ɾ(Right|Left)Projectionͱ͍͏ܕʹੜ͑ͯΔ ɾrightϝιουͰม scala>
Right(“12").right.flatMap(parseInteger) res2: scala.util.Either[NumberFormatException,Int] = Right(12) scala> Right(“tanishi").right.flatMap(parseInteger) res3: scala.util.Either[NumberFormatException,Int] = Left(java.lang.NumberFormatException: For input string: "tanishi") -
͓͠ͳ͕͖ 1. ࣗݾհ 2. scala.Either ֓ཁ 3. scala.Either ͷෆศ 4.
ෆศͷݪҼ 5. scalaz.\/ -
Either - ֬ೝ scala ͷ forࣜίϯύΠϧ࣌ʹ෦తʹ map / flatMap /
withFilter / filter / foreach ͱ͍ͬͨϝιουͷΈ߹Θͤʹม͞ΕΔ -
Ұͭ for { a <- Right(1).right b = a +
1 } yield b -
for-comprehensionͰ ೖͬΆ͍͜ͱ͕Ͱ͖ͳ͍ for { a <- Right(1).right b = a
+ 1 } yield b <console>:24: error: value map is not a member of Product with Serializable with scala.util.Either[Nothing,(Int, Int)] a <- Right(1).right FJUIFS͔Βग़͖ͯͨΛӈࣜʹ࣋ͭ࣌ -
for-comprehensionͰ ύλʔϯϚονͰ͖ͳ͍ for { (a, b) <- Right((1, 2)).right }
yield a + b <console>:19: error: constructor cannot be instantiated to expected type; found : (T1, T2) required: scala.util.Either[Nothing,(Int, Int)] (a, b) <- Right((1, 2)).right ^ -
for-comprehensionͰ ύλʔϯϚονͰ͖ͳ͍ (for { a <- Right(1).right if a >
0 } yield a) match { case Right(num) => println(num) case Left(_) => println("left!!") } <console>:28: error: constructor cannot be instantiated to expected type; found : scala.util.Right[A,B] required: Option[scala.util.Either[Nothing,Int]] case Right(n) => println(n) ύλʔϯϚον͡Όͳ͍͚Ͳ͜Εμϝ -
ͪͳΈʹ྆ํͱ OptionͰ࣮ݱͰ͖Δ for { (a, b) <- Some((1, 2)) }
yield a + b res0: Option[Int] = Some(3) for { a <- Some(1) b = a + 1 } yield b res1: Option[Int] = Some(2) -
͓͠ͳ͕͖ 1. ࣗݾհ 2. scala.Either ֓ཁ 3. scala.Either ͷෆศ 4.
ෆศͷݪҼ 5. scalaz.\/ -
֬ೝ scala.Either scalaz.\/ ʹ withFilter ͕࣮ ͞Ε͓ͯΒͣ withFilter ͷΘΓʹ
filter ͕༻͞ ΕΔ ࠓճͷͰ filter ͱ withFilter ͷҧ͍ॏཁͰ ͳ͍ͷͰΛͭΉΓ·͢ -
for-comprehensionͰ ೖͬΆ͍͜ͱ͕Ͱ͖ͳ͍ for { a <- Right(1).right b = a
+ 1 } yield b ͍͍ͩͨ͜Μͳײ͡ʹͳΔͬΆ͍ Right(1).right.map(a => { val b = a + 1; (a, b) }).map(_ match { case ((a, b)) => b }) -
for-comprehensionͰ ೖͬΆ͍͜ͱ͕Ͱ͖ͳ͍ Right(1).right.map(a => { val b = a +
1; (a, b) }).map(_ match { case ((a, b)) => b }) ͜͜ͷฦΓ Right((1, 2)) -
for-comprehensionͰ ೖͬΆ͍͜ͱ͕Ͱ͖ͳ͍ Right(1).right.map(a => { val b = a +
1; (a, b) }).map(_ match { case ((a, b)) => b }) ͜͜ͷฦΓ Right((1, 2)) map flatMap ͕ੜ͑ͯΔͷ RightProjection!!!!! -
for-comprehensionͰ ೖͬΆ͍͜ͱ͕Ͱ͖ͳ͍ ɾ(Right|Left)Projection.map() ͕ฦ͢ܕ Either ɾ͔͠͠ Either ʹ map
flatMap ੜ͑ͯͳ͍ ɾEither ʹ map ͱ͔͕ੜ͑ͯͨΒྑ͍ͷʹ… -
for-comprehensionͰ ύλʔϯϚονͰ͖ͳ͍ for { (a, b) <- Right((1, 2)).right }
yield a + b ͍͍ͩͨ͜Μͳײ͡ʹͳΔͬΆ͍ Right((1, 2)).right.filter(_ match { case (_, _) => true case _ => false }).map(_ match { case (a, b) => a + b }) -
for-comprehensionͰ ύλʔϯϚονͰ͖ͳ͍ Right((1, 2)).right.filter(_ match { case (_, _) =>
true case _ => false }).map(_ match { case (a, b) => a + b }) λϓϧ͕λϓϧ͔ΛXJUI'JMUFSͰݕࠪ -
for-comprehensionͰ ύλʔϯϚονͰ͖ͳ͍ Right((1, 2)).right.filter(_ match { case (_, _) =>
true case _ => false }).map(_ match { case (a, b) => a + b }) <console>:19: error: constructor cannot be instantiated to expected type; found : (T1, T2) required: scala.util.Either[Nothing,(Int, Int)] (a, b) <- Right((1, 2)).right ^ -
for-comprehensionͰ ύλʔϯϚονͰ͖ͳ͍ RightProjection.filter ͷ࣮ΛݟͯΈΔͱ def filter[X](p: B => Boolean): Option[Either[X,
B]] = e match { case Left(_) => None case Right(b) => if(p(b)) Some(Right(b)) else None } https://github.com/scala/scala/blob/d6f66ec0f38e42c19f79cbe9d32d29c65dee1e05/src/library/ scala/util/Either.scala#L546-L549 -
for-comprehensionͰ ύλʔϯϚονͰ͖ͳ͍ RightProjection.filter ͷ࣮ΛݟͯΈΔͱ def filter[X](p: B => Boolean): Option[Either[X,
B]] = e match { case Left(_) => None case Right(b) => if(p(b)) Some(Right(b)) else None } https://github.com/scala/scala/blob/d6f66ec0f38e42c19f79cbe9d32d29c65dee1e05/src/library/ scala/util/Either.scala#L546-L549 -
for-comprehensionͰ ύλʔϯϚονͰ͖ͳ͍ Right((1, 2)).right.filter(_ match { case (_, _) =>
true case _ => false }).map(_ match { case (a, b) => a + b }) <console>:19: error: constructor cannot be instantiated to expected type; found : (T1, T2) required: scala.util.Either[Nothing,(Int, Int)] (a, b) <- Right((1, 2)).right ^ Either[Nothing, (Int, Int)] -
for-comprehensionͰ ύλʔϯϚονͰ͖ͳ͍ ɾfilter ͕ Option[Either[X,B]] Λฦ͢ͷ͕ݪҼ ɾfilter ͕ Either[X,B] Λฦͯ͘͠ΕΔͱOK
ɾfilter ʹҾ͔͔ͬΒͳ͔ͬͨͱ͖ʹʮࣗવͳʯLeftΛ ఆٛͰ͖Δͱྑͦ͞͏ ɾEither[String, B] ͳΒ Left(“”) ɾEither[Int, B] ͳΒ Left(0) ɾΈ͍ͨʹฦ͢ͱࣗવͬΆ͘ͳ͍ʁ -
for-comprehensionͰ ύλʔϯϚονͰ͖ͳ͍ ͷߟ͍͖͑ͨ͞ΐʔͷfilter(ະ) def filter[X](p: B => Boolean): Either[X, B]
= e match { case Left(e) => Left(e) case Right(b) => if(p(b)) Right(b) ɹɹ else Left(<͍͍ײ͡ͷ>: X) } -
͓͠ͳ͕͖ 1. ࣗݾհ 2. scala.Either ֓ཁ 3. scala.Either ͷෆศ 4.
ෆศͷݪҼ 5. scalaz.\/ -
͜Ε·Ͱͷ·ͱΊ ɾRightProjection Ͱͳ͘ Either ʹ map/flatMap ͕ੜ͑ͯΕخ͍͠ ɾfilter ͕ Either[X,B]
Λฦͯ͠ཉ͍͠ ɾͦͷͨΊ filter ͰϚον͠ͳ͔ͬͨͱ͖ʹࣗવͳ LeftΛͱΔૢ࡞͕Ͱ͖Δͱྑͦ͞͏ -
scalaz.\/ sealed abstract class \/[+A, +B] extends Product with Serializable
final case class -\/[+A](a: A) extends (A \/ Nothing) final case class \/-[+B](b: B) extends (Nothing \/ B) scala.Either ͱҧͬͯ scalaz.\/ ʹ map flatMap ͕ੜ ͑ͯΔʂ -
for-comprehensionͰ ೖͬΆ͍͜ͱ͕Ͱ͖ͳ͍ scalaz.\/ ʹ map flatMap ͕ੜ͑ͯΔʂʂ for {
a <- 1.right[String] b = a + 1 } yield b res3: scalaz.\/[String,Int] = \/-(2) -
for-comprehensionͰ ύλʔϯϚονͰ͖ͳ͍ ͓͞Β͍: RightProjection.filter ͷ࣮ def filter[X](p: B => Boolean):
Option[Either[X, B]] = e match { case Left(_) => None case Right(b) => if(p(b)) Some(Right(b)) else None } -
for-comprehensionͰ ύλʔϯϚονͰ͖ͳ͍ ͷߟ͍͖͑ͨ͞ΐʔͷfilter def filter[X](p: B => Boolean): Either[X, B]
= e match { case Left(e) => Left(e) case Right(b) => if(p(b)) Right(b) ɹɹ else Left(<͍͍ײ͡ͷ>: X) } -
for-comprehensionͰ ύλʔϯϚονͰ͖ͳ͍ scalaz.\/.filter ͷ࣮ def filter[AA >: A](p: B =>
Boolean) (implicit M: Monoid[AA]): (AA \/ B) = this match { case -\/(_) => this case \/-(b) => if(p(b)) this else -\/(M.zero) } https://github.com/scalaz/scalaz/blob/series/7.3.x/core/src/main/scala/scalaz/ Either.scala#L145-L150 -
for-comprehensionͰ ύλʔϯϚονͰ͖ͳ͍ Monoid : (Monad͡Όͳ͍Αʂ) ू߹(ܕ)Sͱͦͷ্ͷೋ߲ԋࢉ |+| ʹ͍ͭͯҎԼͷنଇΛຬͨ͢ ू߹ͱೋ߲ԋࢉͷΈ߹Θͤ 1.
ೋ߲ԋࢉ ͕ू߹ʹ͍ͭͯด͍ͯ͡Δ (ೋ߲ԋࢉͷҾͱฦΓͷܕ͕ಉ͡) 2. ԋࢉ͕݁߹Λຬͨ͢ (a |+| b) |+| c == a |+| (b |+| c) 3. ୯Ґݩe ͕ଘࡏ͢Δ (forall s: S) s |+| e == e && e |+| s == e ࠓճॏཁͳͷ ͜ͷੑ࣭ʂʂ -
for-comprehensionͰ ύλʔϯϚονͰ͖ͳ͍ ྫ: Int ͱ + ͳΒ୯Ґݩ 0 0 +
x = x + 0 = x (where x: Int) String ͱ + ͳΒ୯Ґݩ “” “” + s = s + “” = s (where s: String) -
for-comprehensionͰ ύλʔϯϚονͰ͖ͳ͍ Monoid[X].zero ͰܕXͷ(Ճ๏ʹ͍ͭͯͷ(?))୯ҐݩΛͱΔ Monoid[Int].zero = 0 Monoid[String].zero = “”
Monoid[List[Int]] = List() Ͳ͜Ͱఆٛ͞ΕͯΔͷ͔ -
for-comprehensionͰ ύλʔϯϚονͰ͖ͳ͍ IntΒStringΒ͍ΖΜͳܕͷMonoidͱͯ͠ͷੑ࣭͕ implicit object ͱͯ͠ scalazͰఆٛ͞ΕͯΔ trait StringInstances {
implicit object stringInstance extends Monoid[String] with /*தུ*/ { // தུ def zero: String = "" // தུ } } https://github.com/scalaz/scalaz/blob/series/7.3.x/core/src/main/scala/scalaz/std/ String.scala#L5 -
for-comprehensionͰ ύλʔϯϚονͰ͖ͳ͍ scalaz.\/.filter ͷ࣮ def filter[AA >: A](p: B =>
Boolean) (implicit M: Monoid[AA]): (AA \/ B) = this match { case -\/(_) => this case \/-(b) => if(p(b)) this else -\/(M.zero) } aʹͳΔ͍ΖΜͳܕʹ͍ͭͯ౷Ұతͳ ΠϯλʔϑΣʔε .POPJE<9>[FSP ͰྵݩΛฦ͢͜ͱ͕Ͱ͖Δ -
for-comprehensionͰ ύλʔϯϚονͰ͖ͳ͍ 1.right[String].filter(_ > 0) res25: scalaz.\/[String,Int] = \/-(1) 1.right[String].filter(_
< 0) res26: scalaz.\/[String,Int] = -\/() -
for-comprehensionͰ ύλʔϯϚονͰ͖ͳ͍ for { (a, b) <- (1, 2).right[String] }
yield a + b res27: scalaz.\/[String,Int] = \/-(3) (warning͕ग़·͕͢withFilterͷΘΓʹfilterΛͬͨͧʂͬͯ ͍͏ܯࠂ) OK͡ΌΜʂʂ -
·ͱΊ ɾscala.Either ʹෆࣗવͳڍಈ͕͍͔ͭ͋͘Δ ɾͦͷҰͭͷղܾࡦͱͯ͠ scalaz.\/ Λ͏ͱ͍͏બࢶ͕ ͋Δ ɾ؆୯ͳͱ͜Ζ(\/)͔Β scalaz ͍ͬͯ͘ͷྑͦ͞͏
ɾศརͳͱ͜Ζ͚͓ͩ͏ʂ ɾେࣄͳͷ scala.Either ͷڍಈΛ͓ͬͯ͘͜ͱʂ -
ࢀߟ HaskellͷdoͱScalaͷforࣜͱEitherͱMonadPlus http://d.hatena.ne.jp/xuwei/20130517/1368814058 ओཁͳܕΫϥεͷհ https://gist.github.com/gakuzzzz/8d497609012863b3ea50 scalaz https://github.com/scalaz/scalaz ಠश Scalaz —
\/ http://eed3si9n.com/learning-scalaz/ja/Either.html