@@ -4,8 +4,10 @@ import Prelude
4
4
5
5
import Control.Monad.Eff (Eff )
6
6
import Control.Monad.Eff.Console (log , CONSOLE )
7
+ import Data.Foldable (for_ , foldMapDefaultR , class Foldable , all )
8
+ import Test.Assert (assert )
7
9
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 )
9
11
import Data.Maybe (Maybe (..), isNothing , fromJust )
10
12
import Data.Tuple (Tuple (..))
11
13
@@ -44,8 +46,8 @@ testArray = do
44
46
assert $ replicateM (-1 ) (Just 1 ) == Just []
45
47
46
48
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)
49
51
50
52
-- some
51
53
-- many
@@ -292,6 +294,17 @@ testArray = do
292
294
assert $ foldM (\x y -> Just (x + y)) 0 (range 1 10 ) == Just 55
293
295
assert $ foldM (\_ _ -> Nothing ) 0 (range 1 10 ) == Nothing
294
296
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
+
295
308
nil :: Array Int
296
309
nil = []
297
310
@@ -300,3 +313,15 @@ odd n = n `mod` 2 /= zero
300
313
301
314
doubleAndOrig :: Int -> Array Int
302
315
doubleAndOrig 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