module Relude.Nub
( hashNub
, ordNub
, sortNub
, unstableNub
) where
import Data.Eq (Eq)
import Data.Hashable (Hashable)
import Data.HashSet as HashSet
import Data.Ord (Ord)
import Prelude ((.))
import qualified Data.Set as Set
ordNub :: forall a . (Ord a) => [a] -> [a]
ordNub :: [a] -> [a]
ordNub = Set a -> [a] -> [a]
go Set a
forall a. Set a
Set.empty
where
go :: Set.Set a -> [a] -> [a]
go :: Set a -> [a] -> [a]
go _ [] = []
go s :: Set a
s (x :: a
x:xs :: [a]
xs) =
if a
x a -> Set a -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set a
s
then Set a -> [a] -> [a]
go Set a
s [a]
xs
else a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: Set a -> [a] -> [a]
go (a -> Set a -> Set a
forall a. Ord a => a -> Set a -> Set a
Set.insert a
x Set a
s) [a]
xs
{-# INLINEABLE ordNub #-}
hashNub :: forall a . (Eq a, Hashable a) => [a] -> [a]
hashNub :: [a] -> [a]
hashNub = HashSet a -> [a] -> [a]
go HashSet a
forall a. HashSet a
HashSet.empty
where
go :: HashSet.HashSet a -> [a] -> [a]
go :: HashSet a -> [a] -> [a]
go _ [] = []
go s :: HashSet a
s (x :: a
x:xs :: [a]
xs) =
if a
x a -> HashSet a -> Bool
forall a. (Eq a, Hashable a) => a -> HashSet a -> Bool
`HashSet.member` HashSet a
s
then HashSet a -> [a] -> [a]
go HashSet a
s [a]
xs
else a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: HashSet a -> [a] -> [a]
go (a -> HashSet a -> HashSet a
forall a. (Eq a, Hashable a) => a -> HashSet a -> HashSet a
HashSet.insert a
x HashSet a
s) [a]
xs
{-# INLINEABLE hashNub #-}
sortNub :: (Ord a) => [a] -> [a]
sortNub :: [a] -> [a]
sortNub = Set a -> [a]
forall a. Set a -> [a]
Set.toList (Set a -> [a]) -> ([a] -> Set a) -> [a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Set a
forall a. Ord a => [a] -> Set a
Set.fromList
{-# INLINE sortNub #-}
unstableNub :: (Eq a, Hashable a) => [a] -> [a]
unstableNub :: [a] -> [a]
unstableNub = HashSet a -> [a]
forall a. HashSet a -> [a]
HashSet.toList (HashSet a -> [a]) -> ([a] -> HashSet a) -> [a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> HashSet a
forall a. (Eq a, Hashable a) => [a] -> HashSet a
HashSet.fromList
{-# INLINE unstableNub #-}