Skip to content

Instantly share code, notes, and snippets.

@hasufell hasufell/Lens.hs
Last active Sep 20, 2017

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.