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

feat request: Add popLeftWithValue and popWithValue in Data.Sequence #977

Closed
sanao1006 opened this issue Dec 4, 2023 · 5 comments
Closed

Comments

@sanao1006
Copy link

sanao1006 commented Dec 4, 2023

Proposal

I think it would be useful to have a function in Data.Sequence that deletes the edge value and at the same time retrieves the rest of the Sequence without the edge value.

The naming is tentative, but the function is as follows

-- Return `Nothing` if EmptyL
popLeftWithValue :: Seq a -> Maybe(a, Seq a)

-- Return `Nothing` if EmptyR
popWithValue :: Seq a -> Maybe(Seq a, a)

Also, functions like head, tail, last, and init using Maybe monad would surely be useful to add to Data.Sequence.

What do you think?

@jwaldmann
Copy link
Contributor

we do have views that solve exactly this problem?

https://hackage.haskell.org/package/containers-0.7/docs/Data-Sequence.html#g:7

@meooow25
Copy link
Contributor

meooow25 commented Dec 4, 2023

The naming is tentative...

I have seen these two be commonly called uncons and unsnoc in Haskell libraries.

I think it would be useful to have a function in Data.Sequence that deletes the edge value and at the same time retrieves the rest of the Sequence without the edge value.

There are already two ways to do this:

  • viewl and viewr (as jwaldmann has said)
  • The patterns Empty, :<| and Empty, :|>

But I can see the utility in adding these for Sequence for 1. consistency (with other Haskell structures that define uncons and unsnoc) and 2. convenience of working with the result (for instance, we could say safeLast = fmap snd . unsnoc).

@sanao1006
Copy link
Author

There are already two ways to do this:

Yes, you are right. I think it is possible to implement both unsnoc and uncons by using viewl and viewr.

I would be inclined to create a PR if only the maintainer would allow me to do so.

@jwaldmann
Copy link
Contributor

can you demonstrate (by a concrete example) that the proposed functions help to improve your code?

I think (as a user/teacher) the bar should be quite high for non-essential extensions of the API (where "essential" = "not expressible by functions that are already present")

other uses of uncons: https://hoogle.haskell.org/?hoogle=uncons&scope=set%3Astackage but it seems to me that these don't have views/patterns.

if you want concise code, then this (using patterns) seems quite good:

ghci> import Data.Sequence
ghci> s = Data.Sequence.fromList [1,2,3]
ghci> case s of x :<| xs -> xs

note that you don't need case (somefunction s) of

@sanao1006
Copy link
Author

@jwaldmann

I've thought a lot about your opinion. Indeed, the functions I suggested are functions that can be expressed by existing functions, so if I add these functions, it could compromise the compactness of the library.

I decided that if users want to use these functions, they should implement them themselves in their own projects and use them.

So it seems that it is not necessary to add these functions

Thank you for your valuable opinion!
Please close this issue if no problem.

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

No branches or pull requests

3 participants