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

Pipes.Prelude: smaller type constraints on fold, length, maximum, minimum, ... #128

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

fmap
Copy link

@fmap fmap commented Sep 19, 2014

...null, sum, product, toList, and toListM.

@fmap fmap changed the title Pipes.Prelude: less type constraints on fold, length, maximum, minimum, ... Pipes.Prelude: smaller type constraints on fold, length, maximum, minimum, ... Sep 19, 2014
@Gabriella439
Copy link
Owner

Okay, so I finally had a chance to look at this. So the narrow types are there to prevent accidentally discarding unused values. pipes generally tries to make it hard to do the wrong thing by accident, even if that means making things slightly more difficult to use.

However, there is a solution that gives the best of both worlds, which is to provide alternative fold functions that take any return value and return it alongside the fold's result.

Here is an example:

fold' :: Monad m => (x -> a -> x) -> x -> (x -> b) -> Producer a m r -> m (b, r)

That is designed to work with the foldl library, so that you can write things like:

-- `impurely` and `head` come from `foldl`

impurely fold' head :: Producer a m r -> m (Maybe a, r)

That will work for any return value. All the other folds (like maximum and minimum) could be similarly generalized, although chances are that those generalized versions would go in pipes-extras.

I've also added void to the exports of the Pipes module to make it easier to explicitly discard unused return values.

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.

2 participants