Last active
October 5, 2017 10:09
-
-
Save gelisam/0549eb2a292f86ca2574 to your computer and use it in GitHub Desktop.
Demonstrating how to use the Haxl library.
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 DeriveDataTypeable, GADTs, MultiParamTypeClasses, OverloadedStrings, StandaloneDeriving, TypeFamilies #-} | |
import Control.Monad | |
import Data.Hashable | |
import Data.Typeable | |
import Haxl.Core | |
data DeepThought a where | |
AnswerToLifeTheUniverseAndEverything :: DeepThought Int | |
deriving Typeable | |
deriving instance Show (DeepThought a) | |
deriving instance Eq (DeepThought a) | |
runDeepThought :: DeepThought a -> ResultVar a -> IO () | |
runDeepThought AnswerToLifeTheUniverseAndEverything var = putSuccess var 42 | |
instance DataSourceName DeepThought where | |
dataSourceName _ = "DeepThought" | |
instance Show1 DeepThought where | |
show1 AnswerToLifeTheUniverseAndEverything = "AnswerToLifeTheUniverseAndEverything" | |
instance Hashable (DeepThought a) where | |
hashWithSalt salt AnswerToLifeTheUniverseAndEverything = hashWithSalt salt () | |
instance StateKey DeepThought where | |
data State DeepThought = NoState | |
instance DataSource () DeepThought where | |
fetch _ _ _ reqs = SyncFetch $ do | |
forM_ reqs $ \(BlockedFetch req var) -> runDeepThought req var | |
initialState :: StateStore | |
initialState = stateSet NoState stateEmpty | |
-- | | |
-- >>> main | |
-- 42 | |
main :: IO () | |
main = do | |
myEnv <- initEnv initialState () | |
r <- runHaxl myEnv (dataFetch AnswerToLifeTheUniverseAndEverything) | |
print r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment