I assume it's missing because intersection of unsorted, immutable arrays must have a worst-case run time of Ω(n*m)
without using additional space and Data.Vector
is optimized for performance. If you want, you can write that function yourself, though:
import Data.Vector as V
intersect :: Eq a => V.Vector a -> V.Vector a -> V.Vector a
intersect x = V.filter (`V.elem` x)
Or by using a temporary set data structure to achieve an expected O(n + m)
complexity:
import Data.HashSet as HS
intersect :: (Hashable a, Eq a) => V.Vector a -> V.Vector a -> V.Vector a
intersect x = V.filter (`HS.member` set)
where set = HS.fromList $ V.toList x
If you can afford the extra memory usage, maybe you can use some kind of aggregate type for your data, for example an array for fast random access and a hash trie like Data.HashSet
for fast membership checks and always keep both containers up to date. That way you can reduce the asymptotic complexity for intersection to something like O(min(n, m))