def flatTransform[F[_]: FlatMap, A, B](
t: OptionT[F, A],
f: Option[A] => F[Option[B]]
): OptionT[F, B] = OptionT(t.value.flatMap(f))
@amilkov3 was giving this a try, but a bit confused with OptionT(t.value.flatMap(f)) on the last line. Isn't flatMap not a member of F[Option[A]]?
@abhishek7 it is if you give F a FlatMap constraint, although looking at other methods in OptionT that call flatMap on F it would look more like this:
def flatTransform[B](f: Option[A] => F[Option[B]])(implicit F: Monad[F]): OptionT[F, B] =
OptionT(F.flatMap(value)(f))
Since only flatMap is needed, I'd leave a constraint just for FlatMap.
Edit: but yeah, lots of code in cats uses the instances directly instead of the syntax
@amilkov3 I like the alternative you provided! If it's alright with you, I'll open up a quick PR with that implementation along with a test? Let me know if you, @kubukoz, or anyone else has any other suggestions.
EDIT: I opened up a PR here for convenience if we'd like to have this available sooner rather than later: https://github.com/typelevel/cats/pull/2313
Closed by #2314
Most helpful comment
Since only
flatMapis needed, I'd leave a constraint just forFlatMap.Edit: but yeah, lots of code in cats uses the instances directly instead of the syntax