{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveFoldable #-}

-- | Infinite list
module Haskus.Utils.InfList
   ( InfList (..)
   , toList
   , repeat
   , take
   , replicate
   )
where

import Prelude hiding (take,repeat,replicate)

-- | An infinite list
data InfList a
   = a :> InfList a
   deriving (a -> InfList b -> InfList a
(a -> b) -> InfList a -> InfList b
(forall a b. (a -> b) -> InfList a -> InfList b)
-> (forall a b. a -> InfList b -> InfList a) -> Functor InfList
forall a b. a -> InfList b -> InfList a
forall a b. (a -> b) -> InfList a -> InfList b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> InfList b -> InfList a
$c<$ :: forall a b. a -> InfList b -> InfList a
fmap :: (a -> b) -> InfList a -> InfList b
$cfmap :: forall a b. (a -> b) -> InfList a -> InfList b
Functor,InfList a -> Bool
(a -> m) -> InfList a -> m
(a -> b -> b) -> b -> InfList a -> b
(forall m. Monoid m => InfList m -> m)
-> (forall m a. Monoid m => (a -> m) -> InfList a -> m)
-> (forall m a. Monoid m => (a -> m) -> InfList a -> m)
-> (forall a b. (a -> b -> b) -> b -> InfList a -> b)
-> (forall a b. (a -> b -> b) -> b -> InfList a -> b)
-> (forall b a. (b -> a -> b) -> b -> InfList a -> b)
-> (forall b a. (b -> a -> b) -> b -> InfList a -> b)
-> (forall a. (a -> a -> a) -> InfList a -> a)
-> (forall a. (a -> a -> a) -> InfList a -> a)
-> (forall a. InfList a -> [a])
-> (forall a. InfList a -> Bool)
-> (forall a. InfList a -> Int)
-> (forall a. Eq a => a -> InfList a -> Bool)
-> (forall a. Ord a => InfList a -> a)
-> (forall a. Ord a => InfList a -> a)
-> (forall a. Num a => InfList a -> a)
-> (forall a. Num a => InfList a -> a)
-> Foldable InfList
forall a. Eq a => a -> InfList a -> Bool
forall a. Num a => InfList a -> a
forall a. Ord a => InfList a -> a
forall m. Monoid m => InfList m -> m
forall a. InfList a -> Bool
forall a. InfList a -> Int
forall a. InfList a -> [a]
forall a. (a -> a -> a) -> InfList a -> a
forall m a. Monoid m => (a -> m) -> InfList a -> m
forall b a. (b -> a -> b) -> b -> InfList a -> b
forall a b. (a -> b -> b) -> b -> InfList 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
product :: InfList a -> a
$cproduct :: forall a. Num a => InfList a -> a
sum :: InfList a -> a
$csum :: forall a. Num a => InfList a -> a
minimum :: InfList a -> a
$cminimum :: forall a. Ord a => InfList a -> a
maximum :: InfList a -> a
$cmaximum :: forall a. Ord a => InfList a -> a
elem :: a -> InfList a -> Bool
$celem :: forall a. Eq a => a -> InfList a -> Bool
length :: InfList a -> Int
$clength :: forall a. InfList a -> Int
null :: InfList a -> Bool
$cnull :: forall a. InfList a -> Bool
toList :: InfList a -> [a]
$ctoList :: forall a. InfList a -> [a]
foldl1 :: (a -> a -> a) -> InfList a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> InfList a -> a
foldr1 :: (a -> a -> a) -> InfList a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> InfList a -> a
foldl' :: (b -> a -> b) -> b -> InfList a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> InfList a -> b
foldl :: (b -> a -> b) -> b -> InfList a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> InfList a -> b
foldr' :: (a -> b -> b) -> b -> InfList a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> InfList a -> b
foldr :: (a -> b -> b) -> b -> InfList a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> InfList a -> b
foldMap' :: (a -> m) -> InfList a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> InfList a -> m
foldMap :: (a -> m) -> InfList a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> InfList a -> m
fold :: InfList m -> m
$cfold :: forall m. Monoid m => InfList m -> m
Foldable)

-- | Convert to a standard list
toList :: InfList a -> [a]
toList :: InfList a -> [a]
toList (a
a :> InfList a
as) = a
a a -> [a] -> [a]
forall a. a -> [a] -> [a]
: InfList a -> [a]
forall a. InfList a -> [a]
toList InfList a
as

-- | Take for infinite list
take :: Word -> InfList a -> [a]
take :: Word -> InfList a -> [a]
take Word
0 InfList a
_         = []
take Word
n (a
x :> InfList a
xs) = a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: Word -> InfList a -> [a]
forall a. Word -> InfList a -> [a]
take (Word
nWord -> Word -> Word
forall a. Num a => a -> a -> a
-Word
1) InfList a
xs

-- | Repeat for infinite list
repeat :: a -> InfList a
repeat :: a -> InfList a
repeat a
a = InfList a
go
   where
      go :: InfList a
go = a
a a -> InfList a -> InfList a
forall a. a -> InfList a -> InfList a
:> InfList a
go

-- | Replicate for infinite list
replicate :: Word -> a -> InfList a -> InfList a
replicate :: Word -> a -> InfList a -> InfList a
replicate Word
0 a
_ InfList a
xs = InfList a
xs
replicate Word
n a
a InfList a
xs = a
a a -> InfList a -> InfList a
forall a. a -> InfList a -> InfList a
:> Word -> a -> InfList a -> InfList a
forall a. Word -> a -> InfList a -> InfList a
replicate (Word
nWord -> Word -> Word
forall a. Num a => a -> a -> a
-Word
1) a
a InfList a
xs