Discussion: Add total indexing function to Data.Sequence

Data.Sequence offers index :: Seq a -> Int -> a which throws an error if the index is out of range. I'd like to add something like indexMay :: Seq a -> Int -> Maybe a Aside from the safety factor, indexMay would offer a way to ensure the indexing occurs at a particular time. Much like Data.Vector.indexM, this can help prevent memory leaks. In fact, an analogue of indexM can be implemented in terms of indexMay: indexM :: Applicative m => Seq a -> Int -> m a indexM xs i = case xs `indexMay` i of Nothing -> error "indexM: index out of range" Just x -> pure x

On 30 May 2016 at 07:21, David Feuer
Data.Sequence offers
index :: Seq a -> Int -> a
which throws an error if the index is out of range. I'd like to add something like
indexMay :: Seq a -> Int -> Maybe a
+1 (I personally don't like the "May" prefix used for functions like this as I don't find it very readable, but not enough to argue over the colour of the bikeshed ;-) -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com http://IvanMiljenovic.wordpress.com

I don't like that bikeshed color much either. Go ahead and suggest
something else.
On Sun, May 29, 2016 at 6:12 PM, Ivan Lazar Miljenovic
On 30 May 2016 at 07:21, David Feuer
wrote: Data.Sequence offers
index :: Seq a -> Int -> a
which throws an error if the index is out of range. I'd like to add something like
indexMay :: Seq a -> Int -> Maybe a
+1
(I personally don't like the "May" prefix used for functions like this as I don't find it very readable, but not enough to argue over the colour of the bikeshed ;-)
-- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com http://IvanMiljenovic.wordpress.com

On Sun, May 29, 2016 at 06:28:25PM -0400, David Feuer wrote:
I don't like that bikeshed color much either. Go ahead and suggest something else.
safeIndex? totalIndex? Best solution would be to rename index to unsafeIndex and your proposed function to index, but it's obviously not practicable.

I'm tempted to go with `lookup`, to match Data.IntMap.
On Sun, May 29, 2016 at 6:35 PM, Francesco Ariis
On Sun, May 29, 2016 at 06:28:25PM -0400, David Feuer wrote:
I don't like that bikeshed color much either. Go ahead and suggest something else.
safeIndex? totalIndex? Best solution would be to rename index to unsafeIndex and your proposed function to index, but it's obviously not practicable. _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

I call my list version at: 'at :: (Num i, Ord i) => [a] -> i -> Maybe a'.
I also often use "lookup" as a shorthand for "returns Maybe" (and then
"get" means throws on Nothing).
On Sun, May 29, 2016 at 3:52 PM, David Feuer
I'm tempted to go with `lookup`, to match Data.IntMap.
On Sun, May 29, 2016 at 6:35 PM, Francesco Ariis
wrote: On Sun, May 29, 2016 at 06:28:25PM -0400, David Feuer wrote:
I don't like that bikeshed color much either. Go ahead and suggest something else.
safeIndex? totalIndex? Best solution would be to rename index to unsafeIndex and your proposed function to index, but it's obviously not practicable. _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

lookup works for me.
-Edward
On Sun, May 29, 2016 at 6:52 PM, David Feuer
I'm tempted to go with `lookup`, to match Data.IntMap.
On Sun, May 29, 2016 at 6:35 PM, Francesco Ariis
wrote: On Sun, May 29, 2016 at 06:28:25PM -0400, David Feuer wrote:
I don't like that bikeshed color much either. Go ahead and suggest something else.
safeIndex? totalIndex? Best solution would be to rename index to unsafeIndex and your proposed function to index, but it's obviously not practicable. _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

Oops. Prelude. No I guess it doesn't.
On Sun, May 29, 2016 at 9:40 PM, Edward Kmett
lookup works for me.
-Edward
On Sun, May 29, 2016 at 6:52 PM, David Feuer
wrote: I'm tempted to go with `lookup`, to match Data.IntMap.
On Sun, May 29, 2016 at 6:35 PM, Francesco Ariis
wrote: On Sun, May 29, 2016 at 06:28:25PM -0400, David Feuer wrote:
I don't like that bikeshed color much either. Go ahead and suggest something else.
safeIndex? totalIndex? Best solution would be to rename index to unsafeIndex and your proposed function to index, but it's obviously not practicable. _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

Then again, most of Data.Sequence already clashes with the Prelude.
On Sun, May 29, 2016 at 10:47 PM, Edward Kmett
Oops. Prelude. No I guess it doesn't.
On Sun, May 29, 2016 at 9:40 PM, Edward Kmett
wrote: lookup works for me.
-Edward
On Sun, May 29, 2016 at 6:52 PM, David Feuer
wrote: I'm tempted to go with `lookup`, to match Data.IntMap.
On Sun, May 29, 2016 at 6:35 PM, Francesco Ariis
wrote: On Sun, May 29, 2016 at 06:28:25PM -0400, David Feuer wrote:
I don't like that bikeshed color much either. Go ahead and suggest something else.
safeIndex? totalIndex? Best solution would be to rename index to unsafeIndex and your proposed function to index, but it's obviously not practicable. _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

+1 for lookup. On 30.05.2016 04:48, Edward Kmett wrote:
Then again, most of Data.Sequence already clashes with the Prelude.
On Sun, May 29, 2016 at 10:47 PM, Edward Kmett
mailto:ekmett@gmail.com> wrote: Oops. Prelude. No I guess it doesn't.
On Sun, May 29, 2016 at 9:40 PM, Edward Kmett
mailto:ekmett@gmail.com> wrote: lookup works for me.
-Edward
On Sun, May 29, 2016 at 6:52 PM, David Feuer
mailto:david.feuer@gmail.com> wrote: I'm tempted to go with `lookup`, to match Data.IntMap.
On Sun, May 29, 2016 at 6:35 PM, Francesco Ariis
mailto:fa-ml@ariis.it> wrote: > On Sun, May 29, 2016 at 06:28:25PM -0400, David Feuer wrote: >> I don't like that bikeshed color much either. Go ahead and suggest >> something else. > > safeIndex? totalIndex? Best solution would be to rename index to > unsafeIndex and your proposed function to index, but it's obviously > not practicable. > _______________________________________________ > Libraries mailing list > Libraries@haskell.org mailto:Libraries@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries _______________________________________________ Libraries mailing list Libraries@haskell.org mailto:Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries _______________________________________________ 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/

On Sun, May 29, 2016 at 6:12 PM, Ivan Lazar Miljenovic
On 30 May 2016 at 07:21, David Feuer
wrote: Data.Sequence offers
index :: Seq a -> Int -> a
which throws an error if the index is out of range. I'd like to add something like
indexMay :: Seq a -> Int -> Maybe a
+1
(I personally don't like the "May" prefix used for functions like this as I don't find it very readable, but not enough to argue over the colour of the bikeshed ;-)
I too am +1 for the total function, but -1 for the "May" naming scheme. I'd go with "lookup", since that's what (Int)Map does. I'd also be fine with Vector's (!?) if folks really want a symbolic name. (IME this function tends to work better with an alphabetic name, ymmv) -- Live well, ~wren

+1 from me on 'lookup' and '!?'. Personally, I found the '!?' name
intuitive immediately when I first saw it in Vector, so I'm a fan of it.
Not as sure about 'lookup' because what it does on lists and maps is
different from indexing (ie it works with key-value pairs), but it does
scan significantly better than 'indexMay'.
On Tue, May 31, 2016 at 1:38 AM, wren romano
On Sun, May 29, 2016 at 6:12 PM, Ivan Lazar Miljenovic
wrote: On 30 May 2016 at 07:21, David Feuer
wrote: Data.Sequence offers
index :: Seq a -> Int -> a
which throws an error if the index is out of range. I'd like to add something like
indexMay :: Seq a -> Int -> Maybe a
+1
(I personally don't like the "May" prefix used for functions like this as I don't find it very readable, but not enough to argue over the colour of the bikeshed ;-)
I too am +1 for the total function, but -1 for the "May" naming scheme.
I'd go with "lookup", since that's what (Int)Map does.
I'd also be fine with Vector's (!?) if folks really want a symbolic name. (IME this function tends to work better with an alphabetic name, ymmv)
-- Live well, ~wren _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

How about (!?) like Data.Vector?
On Sun, May 29, 2016 at 5:21 PM, David Feuer
Data.Sequence offers
index :: Seq a -> Int -> a
which throws an error if the index is out of range. I'd like to add something like
indexMay :: Seq a -> Int -> Maybe a
Aside from the safety factor, indexMay would offer a way to ensure the indexing occurs at a particular time. Much like Data.Vector.indexM, this can help prevent memory leaks. In fact, an analogue of indexM can be implemented in terms of indexMay:
indexM :: Applicative m => Seq a -> Int -> m a indexM xs i = case xs `indexMay` i of Nothing -> error "indexM: index out of range" Just x -> pure x

On Sun, May 29, 2016 at 5:42 PM, David Feuer
How about (!?) like Data.Vector?
That's good too, though I think most operators should also have an alphanumeric version for those who either like qualified imports, or use it rarely enough that it's not worth memorizing another operator. Or are already using the Data.Vector one.
participants (8)
-
Andreas Abel
-
David Feuer
-
Edward Kmett
-
Evan Laforge
-
Francesco Ariis
-
Ivan Lazar Miljenovic
-
Tikhon Jelvis
-
wren romano