File: leak.hs

package info (click to toggle)
haskell-control-monad-loop 0.1-14
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 76 kB
  • sloc: haskell: 217; makefile: 2
file content (28 lines) | stat: -rw-r--r-- 635 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
-- Make sure basic loops don't leak memory

import Control.Monad
import Control.Monad.Trans.Class
import Control.Monad.Trans.Loop
import Control.Monad.Trans.State.Strict
import Data.Int (Int64)

count :: Int64 -> IO Int64
count n = iterateLoopT 0 $ \i ->
    if i < n
        then return $! i+1
        else exitWith i

sumLoop :: [Int64] -> Int64
sumLoop list =
    flip execState 0 $ foreach list $ \i -> do
        when (i == 10000000) exit
        lift $ modify' (+i)
  where
    modify' f = do
        x <- get
        put $! f x

main :: IO ()
main = do
    count 100000000 >>= print
    print $ sumLoop [1..10000000] + 10000000