Last active
September 20, 2017 16:41
-
-
Save hasufell/0cab9a1d2f71f33e32b59b1a2e3eb587 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
data Flag = Flag { | |
_flagName :: String | |
, _flagDep :: [Int] | |
} | |
-- If a flag with name 'fname' already exists in the input list, | |
-- add 'fdep' to it. Otherwise append a new Flag to the list with 'fdep' | |
-- being the only item. | |
addFlag :: [Flag] -> String -> Int -> [Flag] | |
addFlag flags fname fdep = undefined |
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
data FlagDeps = FlagDeps { | |
_flagName :: String | |
, _flagDep :: [ExDeps] | |
} | |
deriving (Show) | |
addToFlag :: [FlagDeps] -> String -> ExDeps -> [FlagDeps] | |
addToFlag flags fname' deps' = runST $ do | |
ref <- newSTRef False -- whether we inserted successfully | |
go flags fname' deps' ref | |
where | |
go :: [FlagDeps] -> String -> ExDeps -> STRef s Bool -> ST s [FlagDeps] | |
go [] fname deps ref = do | |
v <- readSTRef ref | |
if v then return [] | |
else return [FlagDeps fname [deps]] | |
go (p@(FlagDeps n ds):fs) fname deps ref | |
| n == fname = do | |
writeSTRef ref True | |
return $ (FlagDeps n (deps:ds)):fs | |
| otherwise = fmap (p:) $ go fs fname deps ref |
Author
hasufell
commented
Sep 20, 2017
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment