I can’t seem to figure out a workaround for this issue i’m having.
I have something like this:
getFilePathForDay :: Day -> IO (Maybe FilePath)
getFilePathForDays date days = do
files <- mapM getFilePathForDay $ iterate (addDays 1) date
return . take days . catMaybes $ files
I am trying to get x amount of valid file paths where x is the number of days I want, but the above code just runs forever. I have ran into this problem before with monads and I was wondering if there is a workaround available for this issue I am having.
Thanks!
The code runs forever because you are trying to sequence an infinite number of side-effecting computations in your call to
mapM.Since you don’t know ahead of time how many of these computations you will need to run (as indicated by your use of
catMaybes), you will have to interleave the calls togetFilePathForDaywith the rest of your computation. This could be done usingunsafeInterleaveIO, but as the name suggests, this is not a recommended approach.You could implement this manually as:
There is probably a more elegant way, but it’s not coming to me right now 🙂