Proposal: Add insertAt to Data.Sequence

I've come up with an implementation of insertAt :: Int -> a -> Seq a -> Seq a -- Defined to be equivalent to insertAt i x xs | i < 0 || i > length xs = error "insertAt: index out of range" | otherwise = take i xs <> singleton x <> drop i xs that inserts the given element at the given index with very little tree restructuring. I came up with the vague notion that it might be interesting to try to do something like this early last year, but I wasn't sure how at the time. I largely forgot about it until today, when someone on StackOverflow pointed to the issue I'd opened and asked if it would be implemented, presumably because he wants it. What do y'all think? David Feuer

On 30 May 2016 at 06:32, David Feuer
I've come up with an implementation of
insertAt :: Int -> a -> Seq a -> Seq a -- Defined to be equivalent to insertAt i x xs | i < 0 || i > length xs = error "insertAt: index out of range" | otherwise = take i xs <> singleton x <> drop i xs
that inserts the given element at the given index with very little tree restructuring. I came up with the vague notion that it might be interesting to try to do something like this early last year, but I wasn't sure how at the time. I largely forgot about it until today, when someone on StackOverflow pointed to the issue I'd opened and asked if it would be implemented, presumably because he wants it. What do y'all think?
The partiality is a little troubling; would it be feasible from a usage point of view to have this function return a Maybe? -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com http://IvanMiljenovic.wordpress.com

No, it would not be feasible to have it return a Maybe; usability
would go down the drain. My preferred total alternative would be to
drop the bounds check in that spec, so
insertAt (-1) 99 [1,2,3] === [99,1,2,3]
insertAt 12 99 [1,2,3] === [1,2,3,99]
The only potential concern is that trying to insert at an out-of-range
index seems likely to be a mistake someone might want to get an error
message about. There are a number of other functions in Data.Sequence
that are partial in similar ways; I'm very open to making them total,
but the previous maintainers were concerned about losing useful
errors.
On Sun, May 29, 2016 at 6:10 PM, Ivan Lazar Miljenovic
On 30 May 2016 at 06:32, David Feuer
wrote: I've come up with an implementation of
insertAt :: Int -> a -> Seq a -> Seq a -- Defined to be equivalent to insertAt i x xs | i < 0 || i > length xs = error "insertAt: index out of range" | otherwise = take i xs <> singleton x <> drop i xs
that inserts the given element at the given index with very little tree restructuring. I came up with the vague notion that it might be interesting to try to do something like this early last year, but I wasn't sure how at the time. I largely forgot about it until today, when someone on StackOverflow pointed to the issue I'd opened and asked if it would be implemented, presumably because he wants it. What do y'all think?
The partiality is a little troubling; would it be feasible from a usage point of view to have this function return a Maybe?
-- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com http://IvanMiljenovic.wordpress.com

On Sun, May 29, 2016 at 3:26 PM, David Feuer
No, it would not be feasible to have it return a Maybe; usability would go down the drain. My preferred total alternative would be to drop the bounds check in that spec, so
This is also what I would expect, by analogy to splitAt (and take and drop).

I rather like the variant without the bounds check that does the right
thing at extremes.
-Edward
On Sun, May 29, 2016 at 6:26 PM, David Feuer
No, it would not be feasible to have it return a Maybe; usability would go down the drain. My preferred total alternative would be to drop the bounds check in that spec, so
insertAt (-1) 99 [1,2,3] === [99,1,2,3] insertAt 12 99 [1,2,3] === [1,2,3,99]
The only potential concern is that trying to insert at an out-of-range index seems likely to be a mistake someone might want to get an error message about. There are a number of other functions in Data.Sequence that are partial in similar ways; I'm very open to making them total, but the previous maintainers were concerned about losing useful errors.
On Sun, May 29, 2016 at 6:10 PM, Ivan Lazar Miljenovic
wrote: On 30 May 2016 at 06:32, David Feuer
wrote: I've come up with an implementation of
insertAt :: Int -> a -> Seq a -> Seq a -- Defined to be equivalent to insertAt i x xs | i < 0 || i > length xs = error "insertAt: index out of range" | otherwise = take i xs <> singleton x <> drop i xs
that inserts the given element at the given index with very little tree restructuring. I came up with the vague notion that it might be interesting to try to do something like this early last year, but I wasn't sure how at the time. I largely forgot about it until today, when someone on StackOverflow pointed to the issue I'd opened and asked if it would be implemented, presumably because he wants it. What do y'all think?
The partiality is a little troubling; would it be feasible from a usage point of view to have this function return a Maybe?
-- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com http://IvanMiljenovic.wordpress.com
Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

On 30.05.2016 00:26, David Feuer wrote:
No, it would not be feasible to have it return a Maybe; usability would go down the drain.
A priori, I cannot see why. It is easy to make a Maybe-partial function "total" by prefixing it with fromMaybe (error "the universe is inconsistent") If you want to insert at the beginning or at the end, I suppose special functions are already in place. Cheers, Andreas
My preferred total alternative would be to drop the bounds check in that spec, so
insertAt (-1) 99 [1,2,3] === [99,1,2,3] insertAt 12 99 [1,2,3] === [1,2,3,99]
The only potential concern is that trying to insert at an out-of-range index seems likely to be a mistake someone might want to get an error message about. There are a number of other functions in Data.Sequence that are partial in similar ways; I'm very open to making them total, but the previous maintainers were concerned about losing useful errors.
On Sun, May 29, 2016 at 6:10 PM, Ivan Lazar Miljenovic
wrote: On 30 May 2016 at 06:32, David Feuer
wrote: I've come up with an implementation of
insertAt :: Int -> a -> Seq a -> Seq a -- Defined to be equivalent to insertAt i x xs | i < 0 || i > length xs = error "insertAt: index out of range" | otherwise = take i xs <> singleton x <> drop i xs
that inserts the given element at the given index with very little tree restructuring. I came up with the vague notion that it might be interesting to try to do something like this early last year, but I wasn't sure how at the time. I largely forgot about it until today, when someone on StackOverflow pointed to the issue I'd opened and asked if it would be implemented, presumably because he wants it. What do y'all think?
The partiality is a little troubling; would it be feasible from a usage point of view to have this function return a Maybe?
-- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com http://IvanMiljenovic.wordpress.com
Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
-- Andreas Abel <>< Du bist der geliebte Mensch. Department of Computer Science and Engineering Chalmers and Gothenburg University, Sweden andreas.abel@gu.se http://www2.tcs.ifi.lmu.de/~abel/
participants (5)
-
Andreas Abel
-
David Feuer
-
Edward Kmett
-
Evan Laforge
-
Ivan Lazar Miljenovic