-
Notifications
You must be signed in to change notification settings - Fork 140
uncons, unsnoc #212
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
uncons, unsnoc #212
Conversation
|
ping |
RyanGlScott
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems sensible to me. However, per the (tacit) conventions of the vector library, you should add versions of uncons and unsnoc to each of the concrete Vector modules:
Data.VectorData.Vector.UnboxedData.Vector.StorableData.Vector.Primitive
|
Done, PTAL |
|
LGTM. @cartazio, thoughts? |
| n' = max n 0 | ||
| len = length v | ||
|
|
||
| -- | /O(1)/ Yield the 'head' and 'tail' of the vector, or 'Nothing' if empty. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why aren't we thinking about how to adapt the fusion codes for head/tail as found in Stream.Monadic and/or bundle?
https://github.com/haskell/vector/blob/master/Data/Vector/Fusion/Stream/Monadic.hs#L341
for one of them
|
|
roughly: ignoring fusion this looks good ... but if we ignore fusion we should just use Array right? :) |
|
@cartazio I think |
|
i think you can look at stream-take and stream-last definitions as starting points perhaps? I swear I saw some machinery for left vs right streams at some point in vector, but i'm not finding it at the moment |
|
https://github.com/haskell/vector/blob/master/Data/Vector/Fusion/Stream/Monadic.hs#L143 has length, not srue if thats quite th right form though |
|
i might be missing something obvious mind you :) |
|
likewise, we probably want to then ahve bundle versions of those operations perhaps? @dolio ? |
|
@cartazio any word on what needs to be done to accept this PR? (e.g. bundle ops) |
|
Reminding me is a great way! I was a bit snowed in intellectual for a
while so I’m climbing over large snow drifts metaphorically
The best way to pull me into prioritizing stuff is to chat with me on
freenode irc.
…On Mon, Mar 25, 2019 at 10:24 PM M Farkas-Dyck ***@***.***> wrote:
@cartazio <https://github.com/cartazio> any word on what needs to be done
to accept this PR? (e.g. bundle ops)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#212 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAAQwifH6JW5L3EOawB6lcYL55Dpc39Qks5vaYTigaJpZM4T0W1l>
.
|
|
@cartazio ping |
|
Derp!
Thx. I’ll poke at this soon.
…On Sat, Nov 23, 2019 at 3:41 PM M Farkas-Dyck ***@***.***> wrote:
@cartazio <https://github.com/cartazio> ping
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#212?email_source=notifications&email_token=AAABBQQH7WMXYEOFT4O4KMDQVGIQZA5CNFSM4E6RNVS2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEE75HQI#issuecomment-557831105>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAABBQVSGWNR2APAJ4VHUGTQVGIQZANCNFSM4E6RNVSQ>
.
|
|
now that the most recent release has been done, i'm catching up on polishing backlog. i'll look |
|
@strake I do support @cartazio push for thinking about fusion. With respect to As far as uncons :: Monad m => Stream m a -> m (Maybe (a, Stream m a))
uncons str@(Stream step t) = unconsMaybeLoop SPEC t
where
unconsMaybeLoop !_ s = do
r <- step s
case r of
Yield x _ -> return $ Just (x, drop 1 str)
Skip s' -> unconsMaybeLoop SPEC s'
Done -> return Nothing
{-# INLINE [0] unconsMaybeLoop #-}
{-# INLINE uncons #-}An even more general and useful approach would be to implement headMaybe (Stream step t) = headMaybeLoop SPEC t
where
headMaybeLoop !_ s = do
r <- step s
case r of
Yield x _ -> return $ Just x
Skip s' -> headMaybeLoop SPEC s'
Done -> return Nothing
{-# INLINE [0] headMaybeLoop #-}
{-# INLINE headMaybe #-}@strake If you'd like to give it a shot that be great if not I'll try it out in a few days myself. |
|
Trying to integrate with fusion only makes it worse. Because we have constant time slicing consecutive uncons/unsonc are very efficient with current implementation. |
|
@strake Thank you for the PR. Sorry it took so long to get it merged. |
Addition of `uncons`, `unsnoc`. Co-authored-by: Alexey Kuleshevich <alexey@kuleshevi.ch>
I often find these useful.