gpt4 book ai didi

haskell - 有什么办法可以使这个 Haskell IO 函数的版本更清晰?

转载 作者:行者123 更新时间:2023-12-01 07:38:09 26 4
gpt4 key购买 nike

我有大量的 IConnection conn => conn -> IO () 函数,我需要执行这些函数才能正确设置数据库。现在,它不是很漂亮,但我是 Haskell 的初学者,无法让它变得更好。

setup :: IConnection conn => conn -> IO ()
setup conn = do
setupUtterances conn
commit conn
setupSegments conn
commit conn
setupLevels conn
commit conn
setupLevelLevel conn
commit conn
setupTCLevelLevel conn
commit conn
setupPaths conn
commit conn
setupLabelTypes conn
commit conn
setupLegalLabels conn
commit conn
setupTracks conn
commit conn
setupVariables conn
commit conn
setupFeatures conn
commit conn
setupAssociations conn
commit conn
return ()

无论如何要缩短它?我在玩
sequence $ map ($ conn) [func1, func2,...]

但我无法让它工作。建议?

最佳答案

刚刚怎么样

setup conn = mapM_ ($ conn) $ intersperse commit
[ setupUtterances,
, setupSegments
, setupLevels
, setupLevelLevel
, setupTCLevelLevel
, setupPaths
, setupLabelTypes
, setupLegalLabels
, setupTracks
, setupVariables
, setupFeatures
, setupAssociations
, \_ -> return ()]
intersperse坚持 commit在所有操作和 mapM_ ($conn) 之间喂食 conn致所有 IO行动。最后 \_ -> return ()是确保 commit在这一切结束时被调用。

关于haskell - 有什么办法可以使这个 Haskell IO 函数的版本更清晰?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25146956/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com