在Haskell中调试时打印时间戳 [英] Printing Timestamps while Debugging in Haskell

查看:228
本文介绍了在Haskell中调试时打印时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还在学习Haskell并调试一些函数,并且通常有一个时间戳函数来了解某些操作何时开始和停止:

I'm still learning Haskell and debugging some functions, and usually have a timestamp function to get a sense of when certain operations start and stop:

doSomeAction :: String -> IO ()
doSomeAction arg1 = do
  putStrLn =<< makeTime
  theThingthatTakesAwhile arg1
  putStrLn =<< makeTime
  where
    makeTime = (formatTime defaultTimeLocale "%Y%m%d%H%M%S") <$> getZonedTime

我的 =<< where 子句包含< $> getZonedTime ?

λ> :t getZonedTime
getZonedTime :: IO ZonedTime

或者这对我或读者有误导性还是非惯用的?

Or is this misleading to me or the reader or non-idiomatic?

输出为:

20170114152312
Doing some long function....
20170114152336

这正是我想看到的---它告诉我我需要什么。看起来很奇怪 putStrLn =<<

Which is exactly what I want to see---it tells me what I need. Just seems odd to putStrLn =<< something to get that effect.

推荐答案

您的代码完全没问题。

但是,按照我自己的风格,我倾向于块:

In my own style, though, I tend to prefer let in do blocks:

doSomeAction :: String -> IO ()
doSomeAction arg1 = do
  let makeTime = formatTime defaultTimeLocale "%Y%m%d%H%M%S" <$> getZonedTime
  putStrLn =<< makeTime
  theThingthatTakesAwhile arg1
  putStrLn =<< makeTime

或者可能是某些变体

doSomeAction :: String -> IO ()
doSomeAction arg1 = do
  let printTime = putStrLn . formatTime defaultTimeLocale "%Y%m%d%H%M%S" =<< getZonedTime
  printTime
  theThingthatTakesAwhile arg1
  printTime

但又是,它是只是个人偏好的问题。

but again, It's only a matter of personal preference.

这篇关于在Haskell中调试时打印时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆