0 | module Data.List.Lazy
13 | data LazyList : Type -> Type where
15 | (::) : (x : a) -> (xs : Lazy (LazyList a)) -> LazyList a
20 | foldrLazy : (func : a -> Lazy acc -> acc) -> (init : Lazy acc) -> (input : LazyList a) -> acc
21 | foldrLazy _ init [] = init
22 | foldrLazy op init (x::xs) = x `op` foldrLazy op init xs
25 | (++) : LazyList a -> Lazy (LazyList a) -> LazyList a
26 | (++) = flip $
foldrLazy (::)
30 | bindLazy : (a -> LazyList b) -> LazyList a -> LazyList b
31 | bindLazy f = foldrLazy ((++) . f) []
34 | choice : Alternative f => LazyList (f a) -> f a
35 | choice = foldrLazy (<|>) empty
38 | choiceMap : Alternative f => (a -> f b) -> LazyList a -> f b
39 | choiceMap g = foldrLazy ((<|>) . g) empty
42 | any : (a -> Bool) -> LazyList a -> Bool
43 | any p = foldrLazy ((||) . p) False
46 | all : (a -> Bool) -> LazyList a -> Bool
47 | all p = foldrLazy ((&&) . p) True
52 | Eq a => Eq (LazyList a) where
54 | x :: xs == y :: ys = x == y && xs == ys
58 | Ord a => Ord (LazyList a) where
60 | compare [] (x :: xs) = LT
61 | compare (x :: xs) [] = GT
62 | compare (x :: xs) (y :: ys)
63 | = case compare x y of
68 | Show a => Show (LazyList a) where
70 | show (h :: t) = "[" ++ show' "" h t ++ "]"
75 | show' : String -> a -> LazyList a -> String
76 | show' acc h Nil = acc ++ show h
77 | show' acc h (x :: xs) = show' (acc ++ show h ++ ", ") x xs
80 | Semigroup (LazyList a) where
81 | xs <+> ys = xs ++ ys
84 | Monoid (LazyList a) where
88 | Foldable LazyList where
89 | foldr op nil [] = nil
90 | foldr op nil (x :: xs) = x `op` foldr op nil xs
92 | foldl op acc [] = acc
93 | foldl op acc (x :: xs) = foldl op (acc `op` x) xs
95 | foldlM fm init xs = foldrLazy (\x, k, z => fm z x >>= k) pure xs init
100 | foldMap f [] = neutral
101 | foldMap f (x :: xs) = f x <+> foldMap f xs
104 | Functor LazyList where
106 | map f (x :: xs) = f x :: map f xs
109 | Applicative LazyList where
111 | fs <*> vs = bindLazy (\f => map f vs) fs
114 | Alternative LazyList where
119 | Monad LazyList where
120 | m >>= f = bindLazy f m
126 | traverse : Monad f => (a -> f b) -> LazyList a -> f (List b)
127 | traverse g [] = pure []
128 | traverse g (x::xs) = pure $
!(g x) :: !(traverse g xs)
130 | public export %inline
131 | for : Monad f => LazyList a -> (a -> f b) -> f (List b)
132 | for = flip traverse
134 | public export %inline
135 | sequence : Monad f => LazyList (f a) -> f (List a)
136 | sequence = traverse id
140 | traverse_ : Monad m => (a -> m b) -> LazyList a -> m Unit
141 | traverse_ f = foldrLazy ((>>) . ignore . f) (pure ())
143 | public export %inline
144 | for_ : Monad m => LazyList a -> (a -> m b) -> m Unit
145 | for_ = flip Lazy.traverse_
147 | public export %inline
148 | sequence_ : Monad m => LazyList (m a) -> m Unit
149 | sequence_ = Lazy.traverse_ id
152 | Zippable LazyList where
153 | zipWith _ [] _ = []
154 | zipWith _ _ [] = []
155 | zipWith f (x::xs) (y::ys) = f x y :: zipWith f xs ys
157 | zipWith3 _ [] _ _ = []
158 | zipWith3 _ _ [] _ = []
159 | zipWith3 _ _ _ [] = []
160 | zipWith3 f (x::xs) (y::ys) (z::zs) = f x y z :: zipWith3 f xs ys zs
162 | unzip xs = (fst <$> xs, snd <$> xs)
163 | unzipWith = unzip .: map
165 | unzip3 xs = (fst <$> xs, fst . snd <$> xs, snd . snd <$> xs)
166 | unzipWith3 = unzip3 .: map
171 | fromList : List a -> LazyList a
173 | fromList (x::xs) = x :: fromList xs
177 | iterate : (f : a -> Maybe a) -> (x : a) -> LazyList a
178 | iterate f x = x :: case f x of
180 | Just y => iterate f y
184 | unfoldr : (b -> Maybe (a, b)) -> b -> LazyList a
185 | unfoldr f c = case f c of
187 | Just (a, n) => a :: unfoldr f n
190 | iterateN : Nat -> (a -> a) -> a -> LazyList a
191 | iterateN Z _ _ = []
192 | iterateN (S n) f x = x :: iterateN n f (f x)
195 | replicate : (n : Nat) -> (x : a) -> LazyList a
197 | replicate (S n) x = x :: replicate n x
202 | head' : LazyList a -> Maybe a
204 | head' (x::_) = Just x
207 | tail' : LazyList a -> Maybe (LazyList a)
209 | tail' (_::xs) = Just xs
214 | take : Nat -> LazyList a -> LazyList a
215 | take (S k) (x::xs) = x :: take k xs
219 | drop : Nat -> LazyList a -> LazyList a
222 | drop (S n) (_::xs) = drop n xs
225 | takeWhile : (a -> Bool) -> LazyList a -> LazyList a
226 | takeWhile p [] = []
227 | takeWhile p (x::xs) = if p x then x :: takeWhile p xs else []
230 | dropWhile : (a -> Bool) -> LazyList a -> LazyList a
231 | dropWhile p [] = []
232 | dropWhile p (x::xs) = if p x then dropWhile p xs else x::xs
235 | filter : (a -> Bool) -> LazyList a -> LazyList a
237 | filter p (x::xs) = if p x then x :: filter p xs else filter p xs
240 | mapMaybe : (a -> Maybe b) -> LazyList a -> LazyList b
242 | mapMaybe f (x::xs) = case f x of
243 | Nothing => mapMaybe f xs
244 | Just y => y :: mapMaybe f xs
249 | take : Fuel -> Stream a -> LazyList a
251 | take (More f) (x :: xs) = x :: take f xs
256 | take : Fuel -> Colist a -> LazyList a
259 | take (More f) (x :: xs) = x :: take f xs
264 | take : Fuel -> Colist1 a -> LazyList a
265 | take fuel as = take fuel (forget as)
270 | mergeReplicate : a -> LazyList a -> LazyList a
271 | mergeReplicate sep [] = []
272 | mergeReplicate sep (y::ys) = sep :: y :: mergeReplicate sep ys
275 | intersperse : a -> LazyList a -> LazyList a
276 | intersperse sep [] = []
277 | intersperse sep (x::xs) = x :: mergeReplicate sep xs
280 | intercalate : (sep : LazyList a) -> (xss : LazyList (LazyList a)) -> LazyList a
281 | intercalate sep xss = choice $
intersperse sep xss
286 | toColist : LazyList a -> Colist a
288 | toColist (x::xs) = x :: toColist xs