Skip to content

Commit 467b7d8

Browse files
committed
Merge pull request #47 from hdgarrood/stack-safe-replicateM
Stack safe replicateM
2 parents a739b20 + a235b86 commit 467b7d8

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/Data/Array.purs

+2-4
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,8 @@ foreign import replicate :: forall a. Int -> a -> Array a
115115

116116
-- | Perform a monadic action `n` times collecting all of the results.
117117
replicateM :: forall m a. (Monad m) => Int -> m a -> m (Array a)
118-
replicateM n m | n < 1 = return []
119-
| otherwise = do a <- m
120-
as <- replicateM (n - 1) m
121-
return (a : as)
118+
replicateM n m | n < 1 = return []
119+
| otherwise = sequence $ replicate n m
122120

123121
-- | Attempt a computation multiple times, requiring at least one success.
124122
-- |

test/Test/Data/Array.purs

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ testArray = do
3131
assert $ replicateM 0 (Just 1) == Just []
3232
assert $ replicateM (-1) (Just 1) == Just []
3333

34+
log "replicateM should be stack safe"
35+
let n = 50000
36+
assert $ replicateM n (Just unit) == Just (replicate n unit)
37+
3438
-- some
3539
-- many
3640

0 commit comments

Comments
 (0)