{-# LANGUAGE DeriveTraversable #-}

module Test.Util.Stream (
    Stream (..)
  , nubOrdBy
  ) where

import qualified Data.Set as Set

data Stream a = a :< Stream a
  deriving ((forall m. Monoid m => Stream m -> m)
-> (forall m a. Monoid m => (a -> m) -> Stream a -> m)
-> (forall m a. Monoid m => (a -> m) -> Stream a -> m)
-> (forall a b. (a -> b -> b) -> b -> Stream a -> b)
-> (forall a b. (a -> b -> b) -> b -> Stream a -> b)
-> (forall b a. (b -> a -> b) -> b -> Stream a -> b)
-> (forall b a. (b -> a -> b) -> b -> Stream a -> b)
-> (forall a. (a -> a -> a) -> Stream a -> a)
-> (forall a. (a -> a -> a) -> Stream a -> a)
-> (forall a. Stream a -> [a])
-> (forall a. Stream a -> Bool)
-> (forall a. Stream a -> Int)
-> (forall a. Eq a => a -> Stream a -> Bool)
-> (forall a. Ord a => Stream a -> a)
-> (forall a. Ord a => Stream a -> a)
-> (forall a. Num a => Stream a -> a)
-> (forall a. Num a => Stream a -> a)
-> Foldable Stream
forall a. Eq a => a -> Stream a -> Bool
forall a. Num a => Stream a -> a
forall a. Ord a => Stream a -> a
forall m. Monoid m => Stream m -> m
forall a. Stream a -> Bool
forall a. Stream a -> Int
forall a. Stream a -> [a]
forall a. (a -> a -> a) -> Stream a -> a
forall m a. Monoid m => (a -> m) -> Stream a -> m
forall b a. (b -> a -> b) -> b -> Stream a -> b
forall a b. (a -> b -> b) -> b -> Stream a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall m. Monoid m => Stream m -> m
fold :: forall m. Monoid m => Stream m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Stream a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Stream a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Stream a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> Stream a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> Stream a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Stream a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Stream a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Stream a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Stream a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Stream a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Stream a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> Stream a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> Stream a -> a
foldr1 :: forall a. (a -> a -> a) -> Stream a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Stream a -> a
foldl1 :: forall a. (a -> a -> a) -> Stream a -> a
$ctoList :: forall a. Stream a -> [a]
toList :: forall a. Stream a -> [a]
$cnull :: forall a. Stream a -> Bool
null :: forall a. Stream a -> Bool
$clength :: forall a. Stream a -> Int
length :: forall a. Stream a -> Int
$celem :: forall a. Eq a => a -> Stream a -> Bool
elem :: forall a. Eq a => a -> Stream a -> Bool
$cmaximum :: forall a. Ord a => Stream a -> a
maximum :: forall a. Ord a => Stream a -> a
$cminimum :: forall a. Ord a => Stream a -> a
minimum :: forall a. Ord a => Stream a -> a
$csum :: forall a. Num a => Stream a -> a
sum :: forall a. Num a => Stream a -> a
$cproduct :: forall a. Num a => Stream a -> a
product :: forall a. Num a => Stream a -> a
Foldable, (forall a b. (a -> b) -> Stream a -> Stream b)
-> (forall a b. a -> Stream b -> Stream a) -> Functor Stream
forall a b. a -> Stream b -> Stream a
forall a b. (a -> b) -> Stream a -> Stream b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> Stream a -> Stream b
fmap :: forall a b. (a -> b) -> Stream a -> Stream b
$c<$ :: forall a b. a -> Stream b -> Stream a
<$ :: forall a b. a -> Stream b -> Stream a
Functor, Int -> Stream a -> ShowS
[Stream a] -> ShowS
Stream a -> String
(Int -> Stream a -> ShowS)
-> (Stream a -> String) -> ([Stream a] -> ShowS) -> Show (Stream a)
forall a. Show a => Int -> Stream a -> ShowS
forall a. Show a => [Stream a] -> ShowS
forall a. Show a => Stream a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> Stream a -> ShowS
showsPrec :: Int -> Stream a -> ShowS
$cshow :: forall a. Show a => Stream a -> String
show :: Stream a -> String
$cshowList :: forall a. Show a => [Stream a] -> ShowS
showList :: [Stream a] -> ShowS
Show, Functor Stream
Foldable Stream
(Functor Stream, Foldable Stream) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> Stream a -> f (Stream b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    Stream (f a) -> f (Stream a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Stream a -> m (Stream b))
-> (forall (m :: * -> *) a.
    Monad m =>
    Stream (m a) -> m (Stream a))
-> Traversable Stream
forall (t :: * -> *).
(Functor t, Foldable t) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => Stream (m a) -> m (Stream a)
forall (f :: * -> *) a.
Applicative f =>
Stream (f a) -> f (Stream a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream a -> m (Stream b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Stream a -> f (Stream b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Stream a -> f (Stream b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Stream a -> f (Stream b)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
Stream (f a) -> f (Stream a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
Stream (f a) -> f (Stream a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream a -> m (Stream b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Stream a -> m (Stream b)
$csequence :: forall (m :: * -> *) a. Monad m => Stream (m a) -> m (Stream a)
sequence :: forall (m :: * -> *) a. Monad m => Stream (m a) -> m (Stream a)
Traversable)

nubOrdBy :: Ord b => (a -> b) -> Set.Set b -> Stream a -> Stream a
nubOrdBy :: forall b a. Ord b => (a -> b) -> Set b -> Stream a -> Stream a
nubOrdBy a -> b
f = Set b -> Stream a -> Stream a
go
  where
    go :: Set b -> Stream a -> Stream a
go Set b
acc (a
x :< Stream a
xs)
      | b -> Set b -> Bool
forall a. Ord a => a -> Set a -> Bool
Set.member (a -> b
f a
x) Set b
acc = Set b -> Stream a -> Stream a
go Set b
acc Stream a
xs
      | Bool
otherwise            = a
x a -> Stream a -> Stream a
forall a. a -> Stream a -> Stream a
:< Set b -> Stream a -> Stream a
go (b -> Set b -> Set b
forall a. Ord a => a -> Set a -> Set a
Set.insert (a -> b
f a
x) Set b
acc) Stream a
xs