-
-
Save joel113/9149c8be0cec486044017c551435d161 to your computer and use it in GitHub Desktop.
Revisions
-
linasm revised this gist
Jan 28, 2019 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -46,6 +46,8 @@ for { y3.flatMap(a => y4.map(b => a * b)) // combining Perhaps with Try: import scala.util.Try for { a <- y4 } yield for { -
linasm revised this gist
Jan 28, 2019 . 1 changed file with 13 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -45,6 +45,19 @@ for { y3.flatMap(a => y4.map(b => a * b)) // combining Perhaps with Try: for { a <- y4 } yield for { b <- Try(100 / a) } yield s"100/$a=$b" // YesItIs(Success(100/4=25)) y4.map { a => Try(100 / a).map { b => s"100/$a=$b" } } // withFilter: for { a <- y3 -
linasm revised this gist
Jan 28, 2019 . 1 changed file with 8 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,17 +6,17 @@ sealed abstract class Perhaps[+A] { } case class YesItIs[A](value: A) extends Perhaps[A] { override def foreach(f: A => Unit): Unit = f(value) override def map[B](f: A => B): Perhaps[B] = YesItIs(f(value)) override def flatMap[B](f: A => Perhaps[B]): Perhaps[B] = f(value) override def withFilter(f: A => Boolean): Perhaps[A] = if (f(value)) this else Nope } case object Nope extends Perhaps[Nothing] { override def foreach(f: Nothing => Unit): Unit = () override def map[B](f: Nothing => B): Perhaps[B] = this override def flatMap[B](f: Nothing => Perhaps[B]): Perhaps[B] = this override def withFilter(f: Nothing => Boolean): Perhaps[Nothing] = this } val y3 = YesItIs(3) -
linasm created this gist
Nov 21, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,69 @@ sealed abstract class Perhaps[+A] { def foreach(f: A => Unit): Unit def map[B](f: A => B): Perhaps[B] def flatMap[B](f: A => Perhaps[B]): Perhaps[B] def withFilter(f: A => Boolean): Perhaps[A] } case class YesItIs[A](value: A) extends Perhaps[A] { override def foreach(f: (A) => Unit): Unit = f(value) override def map[B](f: (A) => B): Perhaps[B] = YesItIs(f(value)) override def flatMap[B](f: (A) => Perhaps[B]): Perhaps[B] = f(value) override def withFilter(f: (A) => Boolean): Perhaps[A] = if (f(value)) this else Nope } case object Nope extends Perhaps[Nothing] { override def foreach(f: (Nothing) => Unit): Unit = () override def map[B](f: (Nothing) => B): Perhaps[B] = this override def flatMap[B](f: (Nothing) => Perhaps[B]): Perhaps[B] = this override def withFilter(f: (Nothing) => Boolean): Perhaps[Nothing] = this } val y3 = YesItIs(3) val y4 = YesItIs(4) val n = Nope // foreach: for { a <- y3 } println(a) y3.foreach(a => println(a)) // map: for { a <- y3 } yield a * a y3.map(a => a * a) // flatMap/map: for { a <- y3 b <- y4 } yield a * b y3.flatMap(a => y4.map(b => a * b)) // withFilter: for { a <- y3 if a > 1 b <- y4 } yield a * b y3.withFilter(a => a > 1).flatMap(a => y4.map(b => a * b)) // assignment: for { a <- y3 b <- y4 c = a * b } yield c y3.flatMap { a => y4.map { b => val c = a * b c } }