You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
return a List[Int] containing both all the elements and their negaঞon. Order is not important. Hint: Given
an element create a list containing it and its negaঞon.
See the soluঞon
I solved it this way :
sealed trait Maybe[A] {
def flatMap[B](fn: A => Maybe[B]): Maybe[B] =
this match {
case Full(v) => fn(v)
case Empty() => Empty[B]()
}
}
final case class Full[A](value: A) extends Maybe[A]
final case class Empty[A]() extends Maybe[A]
val list = List(Full(3), Full(2), Full(1))
list.map(maybe => maybe flatMap { x => if(x % 2 == 0) Full(x) else Empty[Int]() })
the last challenge is this one :
I solved it this way :
where as the given solution is this one :
but that one gives this error :
So did I understand something wrong or is the given solution wrong ?
The text was updated successfully, but these errors were encountered: