How to filter IO [Maybe String]
to only keep the Just
values of the list using >>=
and keep the IO context.
-- returns Just, if the passed binary-name is not present on the system
binDoesntExist :: String -> IO (Maybe String)
binDoesntExist ...
My current solution without the bind-operator:
missingBin :: [String] -> IO [String]
missingBin xs = do
ys <- mapM (\x -> binDoesntExist x) xs
return $ catMaybes ys
I'm currently learning Haskell and try to understand how to use the different functions of the standard library. My solution works, but I guess there is a cleaner way.
A short solution would be
missingBin :: [String] -> IO [String]
missingBin = fmap catMaybes . mapM binDoesntExist
you don't need the >>=
operator for that.
Note: (\x -> binDoesntExist x) = binDoesntExist
From the comment you've written on binDoesntExist
, I suspect that you might rather have a different type signature, namely:
-- returns True if the passed binary is not present on the system
binDoesntExist :: String -> IO Bool
binDoesntExist = ...
The implementation of this signature will probably be simpler than your existing implementation; and additionally your missingBin
will be quite a bit simpler as well:
missingBin :: [String] -> IO [String]
missingBin = filterM binDoesntExist
This discussion assumes that your existing function always returns exactly the String
it's passed (if it returns any String
at all); but this assumption doesn't seem so far out to me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With