Augmented sequence deletion
Zemyla
zemyla at gmail.com
Sat Dec 28 19:59:38 UTC 2019
deleteLookup :: Int -> Seq a -> Maybe (a, Seq a)
deleteLookup n q = case Seq.splitAt n q of
(ql, qr) -> case Seq.viewl qr of
Seq.EmptyL -> Nothing
(Seq.:<) a qr' -> Just (a, ql <> qr')
If it were written natively, it'd probably use some of the machinery from
splitAt.
On 13:25, Sat, Dec 28, 2019 David Feuer <david.feuer at gmail.com wrote:
> Data.Sequence offers
>
> deleteAt :: Int -> Seq a -> Seq a
>
> which deletes the element at the given index. Today, I ran into a
> situation where I wanted to know what was deleted.
>
> deleteLookup :: Int -> Seq a -> Maybe (a, Seq a)
>
> The closest thing I can find in `containers` is in Data.Map:
>
> updateLookupWithKey :: Ord k => (k -> a -> Maybe a) -> k -> Map k a
> -> (Maybe a,Map k a)
>
> Unfortunately, that function is ugly and strange. A better one, whose
> name I can't guess at the moment:
>
> flabbergast :: (a -> (b, Maybe a)) -> Int -> Seq a -> Maybe (b, Seq a)
>
> where a Nothing result means the index was out of bounds. There's also
> a potential
>
> flabbergastF :: Functor f => (a -> f (Maybe a)) -> Int -> Seq a ->
> Maybe (f (Seq a))
>
> I'm not sure if flabbergast can be made as fast as deleteLookup, so
> it's possible we may want both. Any opinions?
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20191228/8ca9ef02/attachment.html>
More information about the Libraries
mailing list