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

Additional pipes prelude functions #174

Open
andrewthad opened this issue Jun 6, 2016 · 5 comments
Open

Additional pipes prelude functions #174

andrewthad opened this issue Jun 6, 2016 · 5 comments

Comments

@andrewthad
Copy link

Would you accept catMaybes and mapMaybe functions that are specialized to work on pipes? They would have the following type signatures:

catMaybes :: Pipe (Maybe a) a m r
mapMaybe :: (a -> b) -> Pipe (Maybe a) b m r

Also worth considering would be lefts and rights, which would be:

lefts :: Pipe (Either a b) a m r
rights :: Pipe (Either a b) b m r
@michaelt
Copy link
Contributor

michaelt commented Jun 6, 2016

Note that catMaybes and rights are both specializations of Pipes.Prelude.concat Maybe the documentation for concat should be elaborated more to bring this out? At the moment it just says

  -- | Flatten all 'Foldable' elements flowing downstream

Pipes.Prelude doesn't really use examples, but something like this might make the meaning of 'flatten' clearer

>>> runEffect $ each [[1], [], [2]] >-> P.concat >-> P.print
1
2
>>> runEffect $ each [Just 1, Nothing, Just 2] >-> P.concat >-> P.print
1
2
>>> runEffect $ each [Right 1, Left "message", Right 2] >-> P.concat >-> P.print
1
2

It's a little annoying, by the way, that the generalization of Prelude.concat to Foldable made the opposite decision

 concat :: Foldable t => t [a] -> [a]

rather than

 concat :: Foldable t =>  [t a] -> [a]

@Gabriella439
Copy link
Owner

In addition to what @michaelt said there is Pipes.Prelude.mapFoldable, too

@mitchellwrosen
Copy link
Contributor

How about intersperse?

intersperse :: Monad m => a -> Producer' a m r -> Producer' a m r

@Gabriella439
Copy link
Owner

@mitchellwrosen You can implement intersperse in terms of other utilities:

interperse x p = for p (\y -> do yield x; yield y) >-> Pipes.Prelude.drop 1

@treeowl
Copy link
Collaborator

treeowl commented Dec 12, 2019

I didn't see this before, but I just added mapMaybe and wither. @michaelt makes a good point about flattening, but I think it's worth adding a now-common idiom.

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

5 participants