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

add unzip to Functor #3023

Closed
julien-truffaut opened this issue Sep 3, 2019 · 10 comments
Closed

add unzip to Functor #3023

julien-truffaut opened this issue Sep 3, 2019 · 10 comments

Comments

@julien-truffaut
Copy link
Contributor

julien-truffaut commented Sep 3, 2019

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

@gagandeepkalra
Copy link
Contributor

Is it okay if I pick this up?

@stremlenye
Copy link

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

@kubukoz
Copy link
Member

kubukoz commented Sep 9, 2019

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

@LukaJCB
Copy link
Member

LukaJCB commented Sep 9, 2019

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

@kubukoz
Copy link
Member

kubukoz commented Sep 9, 2019

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

@kailuowang
Copy link
Contributor

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.

@joroKr21
Copy link
Member

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.

@kubukoz
Copy link
Member

kubukoz commented Sep 10, 2019

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

@joroKr21
Copy link
Member

joroKr21 commented Sep 10, 2019

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

@gagandeepkalra
Copy link
Contributor

gagandeepkalra commented Nov 6, 2019

PR merged, thanks everyone :)

@fthomas fthomas closed this as completed Nov 6, 2019
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

8 participants