Discussion: Add total indexing function to Data.Sequence

David Feuer david.feuer at gmail.com
Sun May 29 21:21:35 UTC 2016

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

More information about the Libraries mailing list