Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type errors on the solution of 5.5.4.3 Sequencing Computaঞons #60

Open
ghost opened this issue Feb 27, 2018 · 0 comments
Open

type errors on the solution of 5.5.4.3 Sequencing Computaঞons #60

ghost opened this issue Feb 27, 2018 · 0 comments

Comments

@ghost
Copy link

ghost commented Feb 27, 2018

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 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants