side effects on the outside • Side effect - an action in addition to return values • FP - Evaluating expressions • Imperative - programs are composed of statements த֩७ਮؔͰͦͷ֎ଆͷͰ෭࡞༻͕࣮ߦ͞ΕΔ '1ࣜΛධՁ͢Δͷʹର͠ɺ໋ྩܕ໋ྩจ͔ΒΔ
value, provided the expression is pure • A function can only be RT if the inputs are also RT • Referential Transparency enables equational reasoning ࢀরಁաੑ 35 ७ਮͳ͕ࣜͦͷͱஔ͖͑Մೳͳ͜ͱ ͕ؔࢀরಁաͰ͋ΔͨΊʹɺೖྗಁաͰ͋Δඞཁ͕͋Δ
Some(i) else None def createMessage(): String = greaterThan5(3).map(x => "Was greater than 5") getOrElse "Was less than or equal to 5" def createMessage2(): String = (if(3 > 5) Some(3) else None).map(x => "Was greater than 5") getOrElse "Was less than or equal to 5" def createMessage3(): String = None.map(x => "Was greater than 5") getOrElse "Was less than or equal to 5" ஔ͖͑Ϟσϧ
be referentially transparent if E can be replaced with its value without changing the behavior of a program • Same effect and output in the end ࢀরಁաੑͷܗࣜԽɿࣜΛͦͷͱஔ͖͑Δ͜ͱ͕Ͱ͖Δ ϓϩάϥϜͷৼΔ͍࡞༻ΛؚΊมΘ͍͚ͬͯͳ͍
A => C we can split it into two functions • Pure function of A => B, where B is the description of the result • Impure function of type B => C which is the interpreter of the description ७ਮͰͳ͍ؔ"㱺$ɺ७ਮؔ"㱺#ͱ ͦͷΠϯλϓϦλ#㱺$ʹ͢Δ͜ͱ͕Մೳ
Option[Person] def result(maybePerson: Option[Person]): String = maybePerson.map { case Person(name, age) => s"${name} is the oldest" } getOrElse "They are the same age" def combine(p1: Person, p2: Person): Unit = println(result(calculateOldest(p1,p2)))