Skip to content

Instantly share code, notes, and snippets.

@hasufell
Last active September 20, 2017 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hasufell/0cab9a1d2f71f33e32b59b1a2e3eb587 to your computer and use it in GitHub Desktop.
Save hasufell/0cab9a1d2f71f33e32b59b1a2e3eb587 to your computer and use it in GitHub Desktop.
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
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
@hasufell
Copy link
Author

hasufell commented Sep 20, 2017

setInsertL x = flip (foldr (\x' go b -> x' : go (b || x == x')) (\b -> if b then [] else [x])) False

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