-
Notifications
You must be signed in to change notification settings - Fork 29
Open
Description
the last challenge is this one :
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]() })
where as the given solution is this one :
list.map(maybe => maybe flatMap { x => if(x % 2 == 0) Full(x) else Empty() })
but that one gives this error :
Error:(15, 135) type mismatch;
found : A$A50.this.Empty.type
required: A$A50.this.Maybe[?]
def get$$instance$$res0 = /* ###worksheet### generated $$end$$ */ list.map(maybe => maybe flatMap { x => if(x % 2 == 0) Full(x) else Empty })
So did I understand something wrong or is the given solution wrong ?
Metadata
Metadata
Assignees
Labels
No labels