Skip to content

Instantly share code, notes, and snippets.

@gdevanla
Last active January 30, 2017 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdevanla/2f6385a74aa15fabda7f1cf5332117df to your computer and use it in GitHub Desktop.
Save gdevanla/2f6385a74aa15fabda7f1cf5332117df to your computer and use it in GitHub Desktop.
Using MVar as a closure value
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE OverloadedStrings #-}
import Control.Concurrent.MVar
testMVar :: IO Int
testMVar = do
value <- newMVar $ ([10, 100, 1000]::[Int])
let f = modifyMVar value $ \l -> case l of
[] -> return ([], 99)
(x:xs) -> return (xs, x)
useF f
--useF :: forall (m :: * -> *) b. (Monad m, Num b) => m b -> m b
useF :: forall (m :: * -> *) b. (Monad m, Num b) => m b -> m b
useF x = do
x1 <- x
x2 <- x
x3 <- x
x4 <- x
return (x1 + x2 + x3 + x4)
main :: IO ()
main = do
--r <- useF $ testMVar
r <- testMVar
putStrLn $ show r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment