Last active
January 30, 2017 04:02
-
-
Save gdevanla/2f6385a74aa15fabda7f1cf5332117df to your computer and use it in GitHub Desktop.
Using MVar as a closure value
This file contains hidden or 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 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