Discussion: Add total indexing function to Data.Sequence

David Feuer david.feuer at gmail.com
Mon May 30 00:42:41 UTC 2016


How about (!?) like Data.Vector?

On Sun, May 29, 2016 at 5:21 PM, David Feuer <david.feuer at gmail.com> 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
>
> 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