Cats: add unzip to Functor

Created on 3 Sep 2019  路  10Comments  路  Source: typelevel/cats

what do you think about adding

trait Functor[F[_]]{
  def unzip[A, B](fab: F[(A, B)]): (F[A], F[B]) = (fab.map(_._1), fab.map(_._2))
}

potentially we could have optimised version per type

reference https://hackage.haskell.org/package/base-4.12.0.0/docs/Data-List-NonEmpty.html#v:unzip

Most helpful comment

Yes you would have to return F[(F[A], F[B])] which defeats the purpose.

All 10 comments

Is it okay if I pick this up?

Wouldn't the implementation @julien-truffaut proposed invoke computation twice for the effect F[_]?

Also concerned about this. This would probably be fine in Foldable, but for effects it'll require evaluating twice or break referential transparency.

We could call it unzipDuplicate or something to make this clear from the naming. WDYT? :)

I'd probably like it more with a comment than that name tbh 馃槅 let's see what others think

The effect duplication seems pretty obvious in type (2 F[A]s out) to me. I agree that if we want to add a note a comment will suffice.

I don't think there is a way to unzip an effect without running it twice unless we do some memoization, but that's cats-effect territory.

@joroKr21 even in cats-effect memoization without an additional effect breaks RT.

Yes you would have to return F[(F[A], F[B])] which defeats the purpose.

PR merged, thanks everyone :)

Was this page helpful?
0 / 5 - 0 ratings