Skip to content

Add mapMaybeM and imapMaybeM - #226

Closed
treeowl wants to merge 1 commit into
haskell:masterfrom
treeowl:mapMaybeM
Closed

treeowl wants to merge 1 commit into
haskell:masterfrom
treeowl:mapMaybeM

Conversation

@treeowl

@treeowl treeowl commented Nov 5, 2018

Copy link
Copy Markdown
Contributor

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

@RyanGlScott RyanGlScott left a comment

Copy link
Copy Markdown
Member

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 :)

Comment thread Data/Vector/Generic.hs
{-# INLINE mapMaybeM #-}
mapMaybeM f = unstreamM . Bundle.mapMaybeM f . stream

imapMaybeM :: (Monad m, Vector v a, Vector v b)

Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread Data/Vector/Generic.hs

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

cartazio commented Nov 6, 2018 via email

Copy link
Copy Markdown
Contributor

@treeowl

treeowl commented Nov 6, 2018

Copy link
Copy Markdown
Contributor Author

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

@treeowl

treeowl commented Nov 6, 2018

Copy link
Copy Markdown
Contributor Author

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

@fumieval

Copy link
Copy Markdown
Contributor

Any updates? The change looks good

@cartazio

cartazio commented Dec 18, 2019 via email

Copy link
Copy Markdown
Contributor

@andrewthad andrewthad mentioned this pull request Jan 7, 2020
@cartazio

Copy link
Copy Markdown
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
Copy Markdown
Contributor

@Shimuuar @lehins any code review suggestions or ideas?

@cartazio

Copy link
Copy Markdown
Contributor

(inclusion for next release,)

@lehins

lehins commented Jan 30, 2020

Copy link
Copy Markdown
Contributor

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

@cartazio

Copy link
Copy Markdown
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

Shimuuar commented Feb 2, 2020

Copy link
Copy Markdown
Contributor

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

lehins commented Feb 2, 2020

Copy link
Copy Markdown
Contributor

@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

Shimuuar commented Feb 2, 2020

Copy link
Copy Markdown
Contributor

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

@treeowl

treeowl commented Feb 2, 2020

Copy link
Copy Markdown
Contributor Author

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

@Shimuuar

Shimuuar commented Feb 2, 2020

Copy link
Copy Markdown
Contributor

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

@Shimuuar

Shimuuar commented Jun 5, 2020

Copy link
Copy Markdown
Contributor

@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
Copy Markdown
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