@@ -10,7 +10,9 @@ module Control.Monad.ListT
1010 , wrapEffect
1111 , wrapLazy
1212 , unfold
13+ , iterate
1314 , fromArray
15+ , toArray
1416 , take
1517 , takeWhile
1618 , drop
@@ -22,6 +24,7 @@ module Control.Monad.ListT
2224 , head
2325 , tail
2426 , foldl
27+ , foldl'
2528 , scanl
2629 , zipWith'
2730 , zipWith
@@ -35,6 +38,9 @@ module Control.Monad.ListT
3538 import Data.Tuple
3639 import qualified Data.Array as A
3740
41+ import Test.QuickCheck
42+ import Test.QuickCheck.LCG
43+
3844 data ListT f a = ListT (f (Step a (ListT f a)))
3945
4046 data Step a s =
@@ -96,6 +102,9 @@ module Control.Monad.ListT
96102 instance monadTransListT :: MonadTrans ListT where
97103 lift = fromEffect
98104
105+ instance arbitraryListT :: (Monad f , Arbitrary a ) => Arbitrary (ListT f a ) where
106+ arbitrary = fromArray <$> arbitrary
107+
99108 singleton :: forall f a. (Applicative f ) => a -> ListT f a
100109 singleton a = prepend a nil
101110
@@ -112,11 +121,18 @@ module Control.Monad.ListT
112121 unfold f z = ListT $ g <$> f z where
113122 g (Just (Tuple z a)) = Yield a (defer \_ -> (unfold f z))
114123 g Nothing = Done
124+
125+ iterate :: forall f a. (Monad f ) => (a -> a ) -> a -> ListT f a
126+ iterate f a = unfold g a where
127+ g a = pure $ Just (Tuple (f a) a )
115128
116129 fromArray :: forall f a. (Monad f ) => [a] -> ListT f a
117130 fromArray xs = unfold f 0 where
118131 f n = pure $ Tuple (n + 1) <$> (xs A .!! n )
119132
133+ toArray :: forall f a. (Monad f ) => ListT f a -> f [a]
134+ toArray = ((<$>) A.reverse ) <<< foldl (flip (:)) []
135+
120136 take :: forall f a. (Applicative f ) => Number -> ListT f a -> ListT f a
121137 take 0 fa = nil
122138 take n fa = stepMap f fa where
@@ -171,6 +187,12 @@ module Control.Monad.ListT
171187 tail :: forall f a. (Monad f ) => ListT f a -> f (Maybe (ListT f a))
172188 tail l = ((<$>) snd ) <$> uncons l
173189
190+ foldl' :: forall f a b. (Monad f ) => (b -> a -> f b ) -> b -> ListT f a -> f b
191+ foldl' f = loop where
192+ loop b l = uncons l >>= g where
193+ g Nothing = pure b
194+ g (Just (Tuple a as)) = (f b a ) >>= (flip loop as )
195+
174196 foldl :: forall f a b. (Monad f ) => (b -> a -> b ) -> b -> ListT f a -> f b
175197 foldl f = loop where
176198 loop b l = uncons l >>= g where
0 commit comments