Created
December 11, 2017 16:25
-
-
Save dgouyette/62a64e6dfc5cd817750f48a6e9d1118d to your computer and use it in GitHub Desktop.
This file contains 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 characters
val t1Int : Option[Int] = Some(5) | |
val t2Int :Option[Int]= Some(6) | |
val t1String : Option[String] = Some("5") | |
val t2String :Option[String]= Some("6") | |
case class MyTuple2[T](a : Option[T], b : Option[T]){ | |
def mapN(f : (T, T)=> T) :Option[T] = { | |
(a, b) match { | |
case (Some(a1), Some(b1))=>Some(f(a1, b1)) | |
case _=> None | |
} | |
} | |
} | |
object MyTuple2 { | |
implicit def t2x[T](t : (Option[T], Option[T]) ) = MyTuple2(t._1, t._2) | |
} | |
import MyTuple2._ | |
(t1Int, None).mapN(_ + _) | |
(t1Int, t2Int).mapN(_ + _) | |
(t1String, t2String).mapN(_ + _) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment