I don't see why you say <-
"decreases readability", but you could line them up if you think that helps (this style is typical of Haskell programmers)
main = forever $ do
diff <- user1 `subtractCounts` user2
timestamp <- formatTime defaultTimeLocale dateFormat <$> getZonedTime
homeDir <- getHomeDirectory
let logMessage = printf "%s %d\n" timestamp diff
logFilePath = homeDir </> logFile
appendFile logFilePath logMessage
threadDelay $ 10^6 * 60 * timeDelay -- in microseconds
subtractCounts = liftM2 ((-) `on` fromMaybe 0) `on` getPlayCount
dateFormat = "[%s] %D %H:%M"
<-
is a visual cue that the thing on the right might to perform an effect before producing a result and binding it to the name on the left.
Note that the type annotation on logMessage
is unnecessary. Because it is used as the second argument to appendFile
it will be inferred to be String
.
I personally might make a few more refactorings, but they aren't really necessary:
logMessage :: String -> Integer -> String
logMessage = printf "%s %d\n"
(f .: g) x y = f $ g x y
subtractCounts = liftM2 ((-) `on` fromMaybe 0) `on` getPlayCount
main = forever $ do
homeDir <- getHomeDirectory
let logFilePath = homeDir </> logFile
logDiff = appendFile logFilePath .: logMessage
forever $ mainLoop logDiff
mainLoop logDiff = do
diff <- user1 `subtractCounts` user2
timestamp <- formatTime defaultTimeLocale "[%s] %D %H:%M" <$> getZonedTime
logDiff timestamp diff
threadDelay $ 10^6 * 60 * timeDelay -- in microseconds
The type signature on logMessage
is probably unnecessary, but imho it's a good idea to give it an explicit signature if you are using the result of printf
as a function, rather than a String
or IO ()
.