Cats: Add `flatTransform` to `OptionT`

Created on 28 Jun 2018  路  5Comments  路  Source: typelevel/cats

def flatTransform[F[_]: FlatMap, A, B](
  t: OptionT[F, A], 
  f: Option[A] => F[Option[B]]
): OptionT[F, B] = OptionT(t.value.flatMap(f))

Most helpful comment

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

All 5 comments

@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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davidabrahams picture davidabrahams  路  3Comments

fosskers picture fosskers  路  3Comments

adelbertc picture adelbertc  路  5Comments

LukaJCB picture LukaJCB  路  4Comments

chuwy picture chuwy  路  4Comments