@@ -4,8 +4,10 @@ import Prelude
44
55import Control.Monad.Eff (Eff )
66import Control.Monad.Eff.Console (log , CONSOLE )
7+ import Data.Foldable (for_ , foldMapDefaultR , class Foldable , all )
8+ import Test.Assert (assert )
79
8- import Data.Array (range , foldM , unzip , zip , zipWithA , zipWith , intersectBy , intersect , (\\), deleteBy , delete , unionBy , union , nubBy , nub , groupBy , group' , group , span , dropWhile , drop , takeWhile , take , sortBy , sort , catMaybes , mapMaybe , filterM , filter , concat , concatMap , reverse , alterAt , modifyAt , updateAt , deleteAt , insertAt , findLastIndex , findIndex , elemLastIndex , elemIndex , (!!), uncons , init , tail , last , head , insertBy , insert , snoc , (:), length , null , replicate , replicateM , singleton )
10+ import Data.Array (range , foldM , unzip , zip , zipWithA , zipWith , intersectBy , intersect , (\\), deleteBy , delete , unionBy , union , nubBy , nub , groupBy , group' , group , span , dropWhile , drop , takeWhile , take , sortBy , sort , catMaybes , mapMaybe , filterM , filter , concat , concatMap , reverse , alterAt , modifyAt , updateAt , deleteAt , insertAt , findLastIndex , findIndex , elemLastIndex , elemIndex , (!!), uncons , init , tail , last , head , insertBy , insert , snoc , (:), length , null , replicate , replicateM , singleton , fromFoldable )
911import Data.Maybe (Maybe (..), isNothing , fromJust )
1012import Data.Tuple (Tuple (..))
1113
@@ -44,8 +46,8 @@ testArray = do
4446 assert $ replicateM (-1 ) (Just 1 ) == Just []
4547
4648 log " replicateM should be stack safe"
47- let n = 50000
48- assert $ replicateM n (Just unit) == Just (replicate n unit)
49+ for_ [ 1 , 1000 , 2000 , 20000 , 50000 ] \n -> do
50+ assert $ replicateM n (Just unit) == Just (replicate n unit)
4951
5052 -- some
5153 -- many
@@ -292,6 +294,17 @@ testArray = do
292294 assert $ foldM (\x y -> Just (x + y)) 0 (range 1 10 ) == Just 55
293295 assert $ foldM (\_ _ -> Nothing ) 0 (range 1 10 ) == Nothing
294296
297+ log " fromFoldable"
298+ for_ [[] , [1 ], [1 ,2 ], [1 ,2 ,3 ,4 ,5 ]] \xs -> do
299+ assert $ fromFoldable xs == xs
300+
301+ log " fromFoldable is stack safe"
302+ for_ [1 , 1000 , 10000 , 20000 , 50000 ] \n -> do
303+ let elem = 0
304+ let arr = fromFoldable (Replicated n elem)
305+ assert $ length arr == n
306+ assert $ all (_ == elem) arr
307+
295308nil :: Array Int
296309nil = []
297310
@@ -300,3 +313,15 @@ odd n = n `mod` 2 /= zero
300313
301314doubleAndOrig :: Int -> Array Int
302315doubleAndOrig x = [x * 2 , x]
316+
317+ data Replicated a = Replicated Int a
318+
319+ instance foldableReplicated :: Foldable Replicated where
320+ foldr f z (Replicated n x) = applyN n (f x) z
321+ foldl f z (Replicated n x) = applyN n (flip f x) z
322+ foldMap = foldMapDefaultR
323+
324+ applyN :: forall a . Int -> (a -> a ) -> a -> a
325+ applyN n f x
326+ | n <= 0 = x
327+ | otherwise = applyN (n - 1 ) f (f x)
0 commit comments