Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix space leak in Shells #381

Merged
merged 1 commit into from
Mar 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/Turtle/Shell.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE RankNTypes #-}
Expand Down Expand Up @@ -101,21 +102,23 @@ data FoldShell a b = forall x . FoldShell (x -> a -> IO x) x (x -> IO b)
-- | A @(Shell a)@ is a protected stream of @a@'s with side effects
newtype Shell a = Shell { _foldShell:: forall r . FoldShell a r -> IO r }

data Maybe' a = Just' !a | Nothing'

translate :: FoldM IO a b -> FoldShell a b
translate (FoldM step begin done) = FoldShell step' Nothing done'
translate (FoldM step begin done) = FoldShell step' Nothing' done'
where
step' Nothing a = do
step' Nothing' a = do
x <- begin
x' <- step x a
return (Just x')
step' (Just x) a = do
return $! Just' x'
step' (Just' x) a = do
x' <- step x a
return (Just x')
return $! Just' x'

done' Nothing = do
done' Nothing' = do
x <- begin
done x
done' (Just x) = do
done' (Just' x) = do
done x

-- | Use a @`FoldM` `IO`@ to reduce the stream of @a@'s produced by a `Shell`
Expand Down