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 more MonoidK based folds #4432

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

BalmungSan
Copy link
Contributor

I am opening this before adding tests and docs to get feedback and see if it is a worth-it addition or not; or even to confirm this doesn't exist already 😅

I found myself needing foldSequenceK today having a List[Resource[IO, HttpRoutes[IO]] and wanting to get back a Resource[IO, HttpRoutes[IO]] in an easy way.

Copy link
Member

@armanbilge armanbilge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, is this foldMapM?

def foldMapM[G[_], A, B](fa: F[A])(f: A => G[B])(implicit G: Monad[G], B: Monoid[B]): G[B] =
foldM(fa, B.empty)((b, a) => G.map(f(a))(B.combine(b, _)))

@BalmungSan
Copy link
Contributor Author

BalmungSan commented Apr 21, 2023

@armanbilge oh lol, you are right. So we were only missing the "friends", right?
Since I originally needed the most derived one (foldSequenceK) I traced back and started from scratch.

Although, I should have realized it, since I first thought of calling foldTraverseK like foldMapMK but thought that name was very confusing and decided to do the parallel with Traverse, also I was still thinking of using Applicative at that moment.

@BalmungSan
Copy link
Contributor Author

BalmungSan commented Apr 21, 2023

Okay, after Arman realized I was reimplementing the wheel with the "traverse" ones.
I just added the "sequence" ones, and changed the name to be consistent... but, being totally honest, I dislike all of them, as well as the M vs A variants, those should be internal optimizations but I understand that was probably not a known idea by the time.
And I especially dislike that foldM takes the usual "fold" parameters instead of being like combineAll, but foldMK doesn't.

Actually, I just realized we are missing something combineAllM, and a foldA version that takes the usual "fold" parameters.

@BalmungSan BalmungSan changed the title WIP: Add foldTraverse & friends WIP: Add more MonoidK based folds Apr 22, 2023
@BalmungSan BalmungSan marked this pull request as ready for review April 24, 2023 14:46
@BalmungSan BalmungSan changed the title WIP: Add more MonoidK based folds Add more MonoidK based folds May 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants