containers icon indicating copy to clipboard operation
containers copied to clipboard

Add takeEnd to Data.Sequence

Open noughtmare opened this issue 3 years ago • 3 comments

The Data.Sequence module is missing the common function takeEnd :: Int -> [a] -> [a] where takeEnd n xs = drop (length xs - n) xs.

I think it is important to include this function because manually calling the length and drop functions can be error prone (I just fixed a bug in my own code).

This function is present in other sequence-like libraries, most notably:

I believe the only reason it is not in Prelude is that this operation is not efficient on lists.

It was almost accepted into Data.Vector, but the implementor didn't have time to finish the work.

noughtmare avatar Dec 21 '22 12:12 noughtmare

Right idea, yes, but is it the right name? I think some libraries list discussion might be in order. The module currently has dropWhileL, dropWhileR, etc. Do we want to use your proposed convention or that one? Personally, I don't care, but I'd love to see what others think.

treeowl avatar Dec 21 '22 13:12 treeowl

Hmm... I think indeed the naming convention should ideally be consistent within this library, so that would mean we would add takeL and takeR (and deprecating take?).

I guess it makes sense that the naming scheme of bytestring and text is asymmetric because their data has a clear left-to-right ordering. In contrast Data.Sequence (and arguably Data.Vector) don't have such a bias.

noughtmare avatar Dec 21 '22 13:12 noughtmare

I don't want to deprecate take and drop; that seems far too disruptive with too little gain. But I'd be open to adding the L/R labeled variants. We're mostly unbiased, but we do follow the original paper in optimizing constant factors for |> and viewl.

treeowl avatar Dec 21 '22 13:12 treeowl

Previously requested in #159. I wouldn't mind adding the R variants.

meooow25 avatar Mar 29 '25 10:03 meooow25