-
Notifications
You must be signed in to change notification settings - Fork 50
Add unsnoc #94
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 unsnoc #94
Conversation
👍 Looks good to me, thanks! |
-- | | ||
-- | Running time: `O(2n)` | ||
unsnoc :: forall a. List a -> Maybe { init :: List a, last :: a } | ||
unsnoc xs = { init: _, last: _ } <$> init xs <*> last xs |
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.
Actually, can we do this in a single pass? Otherwise, the user doesn't gain anything over using the functions separately.
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.
My motivation for adding it was that it makes pattern-matching cleaner. Otherwise, you have to do separate Maybe
checks for both init
and last
. But sure, I'd be happy to optimize it. 😃
|
unsnoc :: forall a. List a -> Maybe { init :: List a, last :: a } | ||
unsnoc lst = unsnocHelper lst Nil <#> \h -> { init: reverse h.revInit, last: h.last } | ||
|
||
unsnocHelper :: forall a. List a -> List a -> Maybe { revInit :: List a, last :: a } |
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.
Could you please move this under a where
? It could be called go
- it's not the best name, but it's consistent with the other implementations here.
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.
Sure! The main reason I split it off was concerns about performance – I figured that redefining go
on every call to unsnoc
was wasteful. But I don't know much about that, so I'll take your advice.
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.
I don't know the extent to which JS engines optimize under functions, but I'd say it's better to be consistent at this point.
👍 Thanks! |
No description provided.