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 foldMapK and reduceMapK #2275

Closed
LukaJCB opened this issue Jun 1, 2018 · 11 comments
Closed

Add foldMapK and reduceMapK #2275

LukaJCB opened this issue Jun 1, 2018 · 11 comments

Comments

@LukaJCB
Copy link
Member

LukaJCB commented Jun 1, 2018

No description provided.

@kubukoz
Copy link
Member

kubukoz commented Jun 3, 2018

foldMapK's signature would look something like this, right?

def foldMapK[G[_], A, B](fga: F[G[A]])(f: A => B)(implicit G: MonoidK[G]): G[B]

(and similarly for reduceMapK, with the change MonoidK -> SemigroupK)

@kubukoz
Copy link
Member

kubukoz commented Jun 3, 2018

hmm, I think I wouldn't be able to implement that without an Applicative[G], which is probably not the point, so I must've got the signature wrong

@kubukoz
Copy link
Member

kubukoz commented Jun 3, 2018

def foldMapK[G[_], A, B](fga: F[A])(f: A => G[B])(implicit G: MonoidK[G]): G[B] =
  foldMap(fa)(f)(G.algebra)

sounds more like it, WDYT?

@LukaJCB
Copy link
Member Author

LukaJCB commented Jun 3, 2018

Yes A => G[B] is what I had in mind :)

@kubukoz
Copy link
Member

kubukoz commented Jun 3, 2018

Cool, I'll work on it then

@kubukoz
Copy link
Member

kubukoz commented Jun 3, 2018

I'm wondering how we can test this.

Should there be a new law that says foldMapK(fa)(f) <-> fa.map(f).foldK? That'd require a Functor[F] in the first place, and there are probably types out there that have a Foldable but not a Functor.

Alternatively, we could have tests for some concrete instance that the equivalence holds, or have a law that reuses what's in the implementation (although I doubt that's the right way to go).

WDYT?

@LukaJCB
Copy link
Member Author

LukaJCB commented Jun 4, 2018

I think a better law would be foldMapK(fa)(f) <-> foldMap(fa)(f)(MonoidK[G].algebra) :)

@kubukoz
Copy link
Member

kubukoz commented Jun 4, 2018

Could be 😅 that's actually what I meant by having a law that reused the implementation we're going to have - but I guess it would suffice for testing people's implementations if they decide to override

@kubukoz
Copy link
Member

kubukoz commented Jun 10, 2018

I'm working on the laws, apparently adding a new type parameter (G[_]) breaks tens of other tests, so bear with me

@LukaJCB
Copy link
Member Author

LukaJCB commented Jun 10, 2018

You could use Id like is done for foldM :)

@kubukoz
Copy link
Member

kubukoz commented Jun 10, 2018

You're telling me now 😂

I already did it for G[_] and passed Option everywhere (as is the custom that I saw in the use sites), in case it's too much to add I can hardcode Id. I'll open a PR now so you can see what it looks like

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants