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 input list transformation functions #3

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

Conversation

gereeter
Copy link

This change adds combinators to change the list that is being folded over. For example, a Fold that multiplies the sum of the even numbers and the sum of the odd numbers in a list could be written like this:

weird :: Fold Int Int
weird = (*) <$> filtered even sum <*> filtered odd sum

Showing it off:

> fold weird [1, 2, 3]
8
> fold weird [1, 2]
2

Without these functions, there was no way to easily do this in one traversal.

@Gabriella439
Copy link
Owner

Sorry for the delay. I'm busy preparing the pipes-4.0.0 release which will be done really soon. I have been doing a lot of thinking about this and I will have more to say on this topic in about a week.

@mitchellwrosen
Copy link
Contributor

Any reason why the functions all have -ed suffixes? It looks like it would be more idiomatic to just call them 'map', 'filter', etc. as foldl was designed to be imported qualified.

Anyways, I came here to make a similar pull request, and, because @Gabriel439 has been thinking hard about this, I'm sure I'm overlooking something =)

@gereeter
Copy link
Author

I added the -ed suffixes because without them, it sounds like I am working on the output of the fold, not the input. The other (possibly better) options would be using premap, prefilter, etc. or using mapInput, filterInput, etc.

Also, I'm glad to see that this is being revived.

@mitchellwrosen
Copy link
Contributor

Hm, it looks like mapped has already been in the repo since 1.0.2, as premap

@Gabriella439
Copy link
Owner

Yeah, premap implements the proposed mapped. Also, I recently added the pretraverse function in foldl-1.0.6:

http://hackage.haskell.org/package/foldl-1.0.6/docs/Control-Foldl.html#v:pretraverse

This function supersedes some of the proposed features here, but not all of them, since it lets you apply a traversal upstream of the fold to pick which targets you want to process. For example, you can now implement the proposed filtered by using pretraverse and the following filtered traversal:

http://hackage.haskell.org/package/lens-4.4.0.1/docs/Control-Lens-Fold.html#v:filtered

I'm still leaving this ticket open because there are several features proposed which have not been implemented yet. I'm not adding them just yet because I'm trying to see if I can find fold them into a more general pattern.

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

Successfully merging this pull request may close these issues.

3 participants