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

tails, inits, initTails example implementation #214

Merged
merged 8 commits into from
Feb 28, 2024

Conversation

BebeSparkelSparkel
Copy link
Collaborator

Here is my example implementation. I have only added:

  • tails, inits, initTails to class IsSequence
  • their default implementations
  • added non-default implementations to the List instance

Can you review for correctness and then I will add it to the other instances?

I am unsure how to add this to tests. How should I add the tests?

closes #213

Copy link
Owner

@snoyberg snoyberg left a comment

Choose a reason for hiding this comment

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

Some test cases for this would be a nice addition if you don't mind. The code looks correct overall.

@BebeSparkelSparkel
Copy link
Collaborator Author

I'll try to add some tests this week

@BebeSparkelSparkel
Copy link
Collaborator Author

Added tests and fixed error for list instance.

Question: Should initTails [] == [([],[])] or initTails == []?

@snoyberg
Copy link
Owner

I honestly have no idea, I've never used that function. Is there any prior art you're aware of for a definition?

@tomjaguarpaw
Copy link

One invariant of this function seems to be that

length xs + 1 == length (initTails xs)

therefore initTails [] == [([], [])] is the correct choice.

@BebeSparkelSparkel
Copy link
Collaborator Author

I have received comment from @tomjaguarpaw above and Atijohn.

Both recommend initTails [] == [([], [])], as now implemented, with good reasoning.

So, this is ready to go.

@@ -472,6 +472,31 @@ class (Monoid seq, MonoTraversable seq, SemiSequence seq, MonoPointed seq) => Is
splitWhen :: (Element seq -> Bool) -> seq -> [seq]
splitWhen = defaultSplitWhen

-- | Returns all the final segments of 'seq' with the longest first.
--
-- @since ????
Copy link
Owner

Choose a reason for hiding this comment

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

Once these are updated, I'll be happy to merge and release. Thanks!

It seems that the definitions for inits and tails differs between String and ByteString.
Benchmarks showed that some specialized instances of initTails based on splitOn were slower than the default definition, so I removed them.
Added reccommended flags for benchmarking.
Added change log entry.
@snoyberg snoyberg merged commit 0447f25 into snoyberg:master Feb 28, 2024
8 checks passed
@snoyberg
Copy link
Owner

Thanks!

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.

Add functions inits, tails, and initTails
3 participants