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

Additional methods for Bitraverse #2474

Closed
asoltysik opened this issue Sep 6, 2018 · 3 comments
Closed

Additional methods for Bitraverse #2474

asoltysik opened this issue Sep 6, 2018 · 3 comments

Comments

@asoltysik
Copy link
Contributor

What do you think about adding any/all of the following functions to Bitraverse? IMHO leftTraverse and leftSequence would be most useful.

def leftTraverse[G[_]: Applicative, A, B, C](fab: F[A, B])(f: A => G[C]): G[F[C, B]] =
  bitraverse(fab)(f, Applicative[G].pure _)

def leftSequence[G[_]: Applicative, A, B](fab: F[G[A], B]): G[F[A, B]] = 
  leftTraverse(fab)(identity)

def biflatTraverse[G[_]: Applicative, A, B, C, D](fab: F[A, B])(f: A => G[F[C]], g: B => G[F[D]])(implicit F: FlatMap[F]): G[F[C, D]] = 
  Applicative[G].bimap(traverse(fab)(f, g))(F.flatten, F.flatten)

def leftFlatTraverse[G[_]: Applicative, A, B, C](fab: F[A, B])(f: A => G[F[C]])(implicit F: FlatMap[F]): G[F[C, B]] = 
  biflatTraverse(fab)(f, identity)

The code above is only a draft, might not compile.

@sideeffffect
Copy link
Contributor

Has biflatTraverse ever been delivered? I'm only able to google this issue and https://gitter.im/typelevel/cats?at=5c78f72f65ffa019ea6b6ae4
Are TypeLevel maintainers open for contribution of biflatTraverse?

@LukaJCB
Copy link
Member

LukaJCB commented Oct 18, 2021

Yeah seems like a good addition to me 👍

@sideeffffect
Copy link
Contributor

sideeffffect commented Oct 20, 2021

I've looked into it briefly and biflatTraverse doesn't seem to be trivially implementable. Just as flatTraverse needs FlatMap[F]

def flatTraverse[G[_], A, B](fa: F[A])(f: A => G[F[B]])(implicit G: Applicative[G], F: FlatMap[F]): G[F[B]]

we would need a totally new BiFlatMap[F] for biflatTraverse to flatten F[F[C, D], F[C, D]] to mere F[C, D].

def biflatTraverse[G[_], A, B, C, D](
    fa: F[A, B]
  )(f: A => G[F[C, D]], g: B => G[F[C, D]])(implicit G: Applicative[G], F: BiflatMap[F]): G[F[C, D]] =
    G.map(bitraverse(fa)(f, g))(F.biflatten)
...

trait BiflatMap[F[_, _]] extends Bifunctor[F] {
  def biflatten[A, B](ffa: F[F[A, B], F[A, B]]): F[A, B]
}

Would you accept a PR with BiflatMap? Seems a bit ad hoc to me, I'm not sure how many usages it would have...

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

3 participants