Skip to content

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

Merged
merged 4 commits into from
Jan 29, 2017
Merged

Add unsnoc #94

merged 4 commits into from
Jan 29, 2017

Conversation

joshuahhh
Copy link
Contributor

No description provided.

@paf31
Copy link
Contributor

paf31 commented Jan 20, 2017

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

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.

Copy link
Contributor Author

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. 😃

@joshuahhh
Copy link
Contributor Author

unsnoc now unsnocs in a single pass, rather than using init and last separately. It is based on the implementation of init. Since it is exactly as performant as init, init is now defined in terms of unsnoc. last, on the other hand, avoids the call to reverse, so I left it the way it is.

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

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

@paf31
Copy link
Contributor

paf31 commented Jan 21, 2017

👍 Thanks!

@paf31 paf31 merged commit 3e746ab into purescript:master Jan 29, 2017
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.

2 participants