Last active
August 4, 2021 20:00
-
-
Save AndrasKovacs/d39105a103abc69a035e6c6c5e176880 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# language ScopedTypeVariables, RankNTypes, BlockArguments, TypeApplications, | |
LambdaCase #-} | |
module Lib | |
( foldr | |
) where | |
import Data.Foldable (foldl') | |
import Prelude hiding (foldr) | |
import Control.Concurrent.MVar | |
import Control.Concurrent | |
import System.IO.Unsafe | |
forEach :: Foldable t => t a -> (a -> IO ()) -> IO () | |
forEach ta f = | |
foldl' (\b a -> seq (unsafeDupablePerformIO (f a)) b) (pure ()) ta | |
foldr :: forall t a b. Foldable t => (a -> b -> b) -> b -> t a -> b | |
foldr f b ta = unsafeDupablePerformIO do | |
elems <- newEmptyMVar @(Maybe a) | |
next <- newEmptyMVar @() | |
forkIO do | |
forEach ta \a -> putMVar elems (Just a) >> takeMVar next | |
putMVar elems Nothing | |
let loop f b = takeMVar elems >>= \case | |
Nothing -> pure b | |
Just a -> do | |
b' <- unsafeInterleaveIO (putMVar next () >> loop f b) | |
pure (f a b') | |
loop f b |
I agree it wasn't thread-safe. Didn't need impure f
for that though, it would've been enough to share a toList
result between threads.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Or just use
unsafeInterleaveIO
there, actually...