Skip to content

Instantly share code, notes, and snippets.

@np
Created February 15, 2018 10:58
Show Gist options
  • Save np/3e9fae5f9c89f3959713d0bd77b30d41 to your computer and use it in GitHub Desktop.
Save np/3e9fae5f9c89f3959713d0bd77b30d41 to your computer and use it in GitHub Desktop.
A pipeline example using MonadJob
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
import Data.Aeson
type URL = String
newtype JobServerURL i o = JobServerURL URL
class Monad m => MonadJob m where
callJob :: (ToJSON i, FromJSON o) => JobServerURL i o -> i -> m o
newtype InputA = C1 () deriving (Eq, Show, ToJSON, FromJSON)
newtype OutputA = C2 () deriving (Eq, Show, ToJSON, FromJSON)
newtype InputB = C3 () deriving (Eq, Show, ToJSON, FromJSON)
newtype OutputB = C4 () deriving (Eq, Show, ToJSON, FromJSON)
newtype InputC = C5 () deriving (Eq, Show, ToJSON, FromJSON)
newtype OutputC = C6 () deriving (Eq, Show, ToJSON, FromJSON)
newtype Input = C7 () deriving (Eq, Show, ToJSON, FromJSON)
newtype Output = C8 () deriving (Eq, Show, ToJSON, FromJSON)
f1 :: Input -> InputA
f2 :: Input -> OutputA -> InputB
f3 :: Input -> OutputA -> OutputB -> InputC
f4 :: Input -> OutputA -> OutputB -> OutputC -> Output
(f1, f2, f3, f4) = undefined
serverA :: JobServerURL InputA OutputA
serverA = JobServerURL "http://hostA/taskA"
serverB :: JobServerURL InputB OutputB
serverB = JobServerURL "http://hostB/taskB"
serverC :: JobServerURL InputC OutputC
serverC = JobServerURL "http://hostC/taskC"
f :: MonadJob m => Input -> m Output
f data1 = do
data2 <- callJob serverA (f1 data1)
data3 <- callJob serverB (f2 data1 data2)
data4 <- callJob serverC (f3 data1 data2 data3)
return $ f4 data1 data2 data3 data4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment