haskell - Removing duplicate elements in a Seq -
wondering how implement nub on seq a
i 1 do:
nubseq :: seq -> seq nubseq = fromlist . nub . tolist just wondering there standard not convert lists in order call nub :: [a]->[a]? 
an implementation occurred me, based on nub, is:
nubseq :: (eq a) => seq -> seq nubseq = data.sequence.foldrwithindex             (\_ x -> case x `data.sequence.elemindexr` of                         _ ->                         nothing -> |> x) data.sequence.empty  but there must more elegant?
thanks.
not sure whether qualifies more elegant splits concerns in independent functions (caveat: need ord constraint on a):
- seqtonubmaptakes- seq, outputs- mapassociating each- asmallest index @ appeared in sequence
- maptolisttakes- mapof values , positions , produces list of values in increasing order according specified positions
- nubseqcombines these generate sequence without duplicates
the whole thing should o(n*log(n)), believe:
module nubseq  import data.map      map import data.list     list import data.sequence seq import data.function  seqtonubmap :: ord => seq -> map int seqtonubmap = foldlwithindex (\ m k v -> insertwith min v k m) map.empty  maptolist :: ord => map int -> [a] maptolist = fmap fst . list.sortby (compare `on` snd) . map.tolist  nubseq :: ord => seq -> seq nubseq = seq.fromlist . maptolist . seqtonubmap or simpler alternative following @davidfletcher's comment:
nubseq' :: forall a. ord => seq -> seq nubseq' xs = fold.foldr cons nil xs set.empty    cons :: -> (set -> seq a) -> (set -> seq a)   cons x xs seen     | x `elem` seen = xs seen     | otherwise     = x <| xs (set.insert x seen)    nil :: set -> seq   nil _ = seq.empty 
Comments
Post a Comment