Simplify process

Note to self: This is a lot like foldr or traverse, and would be trivial to
implement as such if it didn't need to peek ahead when takesArg is true. I
wonder if there's a clean way to implement it in terms of one of them anyway.
This commit is contained in:
Joseph C. Sible 2020-04-05 16:38:52 -04:00
parent fb55072302
commit d22e0aa4a7
1 changed files with 8 additions and 13 deletions

View File

@ -919,20 +919,15 @@ getOpts string flags = process flags
flagMap = Map.fromList $ ("", False) : flagList string flagMap = Map.fromList $ ("", False) : flagList string
process [] = return [] process [] = return []
process [(token, flag)] = do process ((token1, flag):rest1) = do
takesArg <- Map.lookup flag flagMap takesArg <- Map.lookup flag flagMap
guard $ not takesArg (token, rest) <- if takesArg
return [(flag, token)] then case rest1 of
process ((token1, flag1):rest2@((token2, flag2):rest)) = do (token2, ""):rest2 -> return (token2, rest2)
takesArg <- Map.lookup flag1 flagMap _ -> fail "takesArg without valid arg"
if takesArg else return (token1, rest1)
then do more <- process rest
guard $ null flag2 return $ (flag, token) : more
more <- process rest
return $ (flag1, token2) : more
else do
more <- process rest2
return $ (flag1, token1) : more
supportsArrays Bash = True supportsArrays Bash = True
supportsArrays Ksh = True supportsArrays Ksh = True