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 mapMaybeM and imapMaybeM #226

Closed
wants to merge 1 commit into from
Closed

Conversation

treeowl
Copy link
Contributor

@treeowl treeowl commented Nov 5, 2018

Add

mapMaybeM :: Monad m => (a -> m (Maybe b)) -> Vector a -> m (Vector b)
imapMaybeM :: Monad m => (Int -> a -> m (Maybe b)) -> Vector a -> m (Vector b)

mapMaybeM is similar to wither, but the stream fusion framework
seems to require that we use a Monad constraint rather than an
Applicative one to get good performance. imapMaybeM is the
indexed variant.

Resolves #183

Add

```haskell
mapMaybeM :: Monad m => (a -> m (Maybe b)) -> Vector a -> m (Vector b)
imapMaybeM :: Monad m => (Int -> a -> m (Maybe b)) -> Vector a -> m (Vector b)
```

`mapMaybeM` is similar to `wither`, but the stream fusion framework
seems to require that we use a `Monad` constraint rather than an
`Applicative` one to get good performance. `imapMaybeM` is the
indexed variant.

Resolves haskell#183
Copy link
Member

@RyanGlScott RyanGlScott left a comment

Choose a reason for hiding this comment

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

This looks like a fine addition to me! (I'm sort of surprised that we didn't have this already, in fact.)

I've left two suggestions inline, although you probably know this stuff better than I do, so answering "no" to my requests is a perfectly reasonable suggestion :)

{-# INLINE mapMaybeM #-}
mapMaybeM f = unstreamM . Bundle.mapMaybeM f . stream

imapMaybeM :: (Monad m, Vector v a, Vector v b)
Copy link
Member

Choose a reason for hiding this comment

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

Is it worthwhile to mark imapMaybeM as INLINE? (Genuine question—I'm not sure if it would be beneficial here or not.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I sure hope that helps. @bgamari has pointed out some trouble fusing unstreamM, but we surely want to fuse on the stream side.


imapMaybeM :: (Monad m, Vector v a, Vector v b)
=> (Int -> a -> m (Maybe b)) -> v a -> m (v b)
imapMaybeM f = unstreamM . Bundle.mapMaybeM (\(i, a) -> f i a) . Bundle.indexed . stream
Copy link
Member

Choose a reason for hiding this comment

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

Other indexed functions in this module seem to use uncurry f instead of (\(i, a) -> f i a)—perhaps it would be worth sticking to this convention? (I'm not sure if using one or the other has any actual ramifications on strictness properties.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I never use uncurry because it introduces laziness I virtually never need. I bet the compiler often fixes that, but I see no reason to tempt fate.

Copy link
Member

Choose a reason for hiding this comment

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

i wonder if the uncurry thing is actually necessary in general. i tend to prefer not using it as well.

@cartazio
Copy link
Contributor

cartazio commented Nov 6, 2018 via email

@treeowl
Copy link
Contributor Author

treeowl commented Nov 6, 2018

@cartazio, it actually shouldn't require terribly much review. The whole thing is an extremely gentle modification of filterM.

@treeowl
Copy link
Contributor Author

treeowl commented Nov 6, 2018

FYI, the next release of witherable will offer a witherM method in Witherable to take advantage of this sort of thing.

@fumieval
Copy link
Contributor

Any updates? The change looks good

@cartazio
Copy link
Contributor

cartazio commented Dec 18, 2019 via email

@andrewthad andrewthad mentioned this pull request Jan 7, 2020
@cartazio
Copy link
Contributor

this is an often requested function, if we can add it to part of the test suite that would make me game for inclusion

@cartazio
Copy link
Contributor

@Shimuuar @lehins any code review suggestions or ideas?

@cartazio
Copy link
Contributor

(inclusion for next release,)

@lehins
Copy link
Contributor

lehins commented Jan 30, 2020

Adding documentation to those new functions would be a great idea. Otherwise LGTM

@cartazio
Copy link
Contributor

ok, i'm game for merging this in as long as we gate the release on a contrib of docs and tests from whomever

@Shimuuar
Copy link
Contributor

Shimuuar commented Feb 2, 2020

Only missing piece for this PR is documentation. I think easiest approach is to merge it as it is and I'll write haddock for functions later

@lehins
Copy link
Contributor

lehins commented Feb 2, 2020

@Shimuuar if you'd rather not merge it into master without documentation, we could create a temporary branch from master for this PR and merge it into that branch, where you could finish up the doc. All that is necessary is creating a branch and changing the target of this PR

@Shimuuar
Copy link
Contributor

Shimuuar commented Feb 2, 2020

I propose to merge it without documentation and it later separately. Seems like easiest approach

@treeowl
Copy link
Contributor Author

treeowl commented Feb 2, 2020

Do I need to write documentation today? Doesn't sound like a big deal.

@Shimuuar
Copy link
Contributor

Shimuuar commented Feb 2, 2020

No hurry. It's open for more than year. It could wait for few days

@Shimuuar
Copy link
Contributor

Shimuuar commented Jun 5, 2020

@treeowl Could you please add haddocks and changelog entry? So this PR could be finally merged

@Shimuuar Shimuuar mentioned this pull request Oct 10, 2020
@Shimuuar
Copy link
Contributor

Superseded by #333 which is this PR rebased on top of master and added haddocks

@Shimuuar Shimuuar closed this Oct 10, 2020
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.

mapMaybeM
7 participants