I just started learning Haskell again (i've already tried a few times) and i wrote a little function, that is supposed to download a file to a given filepath.
If the URL is Some
it should download the file and if it's Nothing
it should just do nothing.
downloadFile :: URL -> FilePath -> IO ()
downloadFile url fp = ...
maybeDownloadFile :: Maybe URL -> FilePath -> IO ()
maybeDownloadFile ( Just url ) fp = downloadFile url fp
maybeDownloadFile Nothing fp = return ()
So it essentially just wraps the downloadFile
function, but with a Maybe URL
instead of a URL
.
The thing is, I feel like this can be achieved more elegantly with a monadic operator (or maybe with functors) and without the match expression.
Is there some operator like that?
Taking some action on Just
, and doing nothing on Nothing
, is exactly what traverse_
(or mapM_
) does. Flip your arguments to make it even prettier.
downloadFile :: FilePath -> URL -> IO ()
downloadFile fp url = ...
maybeDownloadFile :: FilePath -> Maybe URL -> IO ()
maybeDownloadFile = traverse_ . downloadFile