Skip to content

Instantly share code, notes, and snippets.

@rjregenold
Created August 15, 2013 22:07
Show Gist options
  • Save rjregenold/6245419 to your computer and use it in GitHub Desktop.
Save rjregenold/6245419 to your computer and use it in GitHub Desktop.
module Bus where
import Data.List (find, intercalate)
data BuildStatus = AllGreen
| Borken
deriving (Eq, Show)
data Brogrammer = Johnny
| Arthur
| Bryan
| Nand
| RJ
deriving (Eq, Show)
data Team = Rails
| Mobile
| Go
| Backend
deriving (Show)
data Bus = Bus
{ team :: Team -- ^ the Team this Bus belongs to.
, bros :: [Brogrammer] -- ^ the Brogrammers on board.
}
deriving (Show)
mkString = intercalate " "
-- Setup the BookShout! buses.
railsBus = Bus Rails [Johnny, Bryan]
mobileBus = Bus Mobile [Arthur]
goBus = Bus Go [Nand]
backendBus = Bus Backend [RJ]
-- |Given a Bus, finds who broke the build. Always returns RJ.
whoBrokeTheBuild :: Bus -> Brogrammer
whoBrokeTheBuild = maybe RJ id . find (==RJ) . bros
-- |Determines Jenkins happiness level for the given Bus.
isJenkinsSad :: Bus -> Bool
-- arthur writes flawless code
isJenkinsSad (Bus Mobile _) = False
-- everything else is always borken
isJenkinsSad _ = True
-- |Checks whether or not Jenkins is sad and returns the approriate build status.
buildStatus :: Bus -> BuildStatus
buildStatus bus = if (isJenkinsSad bus) then Borken
else AllGreen
-- |Generates a displayable String based on the build status of the given Bus.
throwUnderBus :: Bus -> String
throwUnderBus bus = mkString $ case buildStatus bus of
Borken ->
[ "Who broke it?"
, innocent
, guilty
, "did."
]
AllGreen ->
[ "If it were broken, who would've done it?"
, innocent
, guilty
, "would."
]
where
guilty = show $ whoBrokeTheBuild bus
innocent = mkString $ map ((++".") . ("Not "++) . show) $ filter (/=RJ) (bros bus)
main = do
performCheck "Let's check on the Rails build." railsBus
performCheck "Now how about the mobile build?" mobileBus
performCheck "What about Go?" goBus
performCheck "Finally, the backend." backendBus
where
status bus = mkString
[ show $ team bus
, "build is"
, show $ buildStatus bus
]
performCheck msg bus = putStrLn msg
>> putStrLn (status bus)
>> putStrLn (throwUnderBus bus)
{-
Running this program produces the following output:
Let's check on the Rails build.
Rails build is Borken
Who broke it? Not Johnny. Not Bryan. RJ did.
Now how about the mobile build?
Mobile build is AllGreen
If it were broken, who would've done it? Not Arthur. RJ would.
What about Go?
Go build is Borken
Who broke it? Not Nand. RJ did.
Finally, the backend.
Backend build is Borken
Who broke it? RJ did.
-}
@bryanmikaelian
Copy link

👍 🚌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment