konsumlamm / rrb-vector

An implementation of a Relaxed Radix Balanced Vector in Haskell.
BSD 3-Clause "New" or "Revised" License
20 stars 5 forks source link

Pattern synonyms? #1

Closed ngua closed 3 years ago

ngua commented 3 years ago

Hi, @konsumlamm, thanks for publishing rrb-vector. I'd like to try it out as a replacement for Seqs in a few projects I'm working on. When working with Seqs, I find using the bundled pattern synonyms quite convenient. Would you be interested in adding similar pattern synonyms for Vector (I didn't see any in the haddocks)? If you think it might be a good choice, I could try to implement them and open a PR. Thanks!

konsumlamm commented 3 years ago

I intentionally didn't add pattern synonyms for viewing the front/back, since viewl/viewr and <|/|> don't run in truly constant time (as opposed to the Data.Sequence counterparts). This could lead to confusion, when people expect pattern matching to have negligible cost. As an alternative, you could use the ViewPatterns extension, for example:

updateHead :: (a -> a) -> Vector a -> Vector a
updateHead f (viewl -> Just (x, xs)) = f x <| xs
updateHead _ xs = xs

updateLast :: (a -> a) -> Vector a -> Vector a
updateLast f (viewr -> Just (xs, x)) = xs |> f x
updateLast _ xs = xs

However, if you're mostly using the Seqs as queue/deque (i.e. you're mostly using viewl/viewr and <|/|>), I don't recommend switching, my Vector will likely be slower. Feel free to share your experience with replacing Seq by Vector though!

ngua commented 3 years ago

Ah, I see, that makes a lot of sense. ViewPatterns are a great alternative, though, thanks for the suggestion. Anyway, I'll go ahead and close this now. Thanks again!