scala-exercises / exercises-cats

Scala Exercises' lessons for the Cats library
Apache License 2.0
107 stars 97 forks source link

Monad & Foldable exercises feedback #37

Open ggalmazor opened 8 years ago

ggalmazor commented 8 years ago

I'm opening this issue just to give some feedback on these two exercises.

It would be nice to explain why OptionT[F, A] & MonoidK[F, A] instead of OptionK[F, A] & MonoidK[F, A] or OptionT[F, A] & MonoidT[F, A]. Don't both suffixes mean the same thing?

Foldable[List].foldK(List(None, Option("two"), Option("three"))) should be(Some("two"))
tr4rex commented 6 years ago

@ggalmazor For the last point - it depends on how the values inside foldable are composed. According to the explanation in exercise:

fold, also called combineAll, combines every value in the foldable using the given Monoid instance

foldK is similar to fold but combines every value in the foldable using the given MonoidK[G] instance instead of Monoid[G]

And for Option instance combineK is defined as follows:

def combineK[A](x: Option[A], y: Option[A]): Option[A] = x orElse y

where orElse is:

  @inline final def orElse[B >: A](alternative: => Option[B]): Option[B] =
    if (isEmpty) alternative else this

So when it goes about combining a bunch of Options, the 1st non-empty values would be taken

ggalmazor commented 6 years ago

Thanks for the explanation, @tr4rex!

Although it's been some time since I did the exercises, I can see what you say :)

Thanks!