Created
November 1, 2012 09:19
-
-
Save dpwiz/3992670 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
-- * Stuff | |
import Database.SQL (connectDB, dbquery) | |
import Database.Cache (cacheGet, cacheSet) | |
import Network.SOAP (soap) | |
import Control.Monad (guard) | |
type Oven = String | |
type Product = String | |
type HaveGas = Bool | |
data Recipe = Recipe { getOven :: Oven | |
, getProduct :: Product | |
, executeWith :: HaveGas -> Maybe Product | |
} | |
haveOven :: Oven -> IO Bool | |
haveOven oven = (elem oven . lines) `fmap` readFile "ovens.txt" | |
haveProduct :: Product -> IO Bool | |
haveProduct prod = connectDB >>= ((> 0) . length) `fmap` dbquery "SELECT 1 FROM products WHERE alias=%s" [prod] | |
gasReady :: IO HaveGas | |
gasReady = cached "gas" 10 $ soap "GasProvider" "gasReady" | |
create :: Recipe -> IO (Maybe Product) | |
create recipe = do | |
haveOven (getOven recipe) >>= guard | |
haveProduct (getProduct recipe) >>= guard | |
gas <- gasReady | |
return $ recipe `executeWith` gas | |
-- * Helpers | |
cached :: String -> Int -> IO a -> a | |
cached key time refresh = do | |
cached <- cacheGet key | |
case cached of | |
Just value -> return value | |
Nothing -> refresh >>= cacheSet key time >>= return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment