作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Haskell 中有以下功能:
memdb = -- load the contents of a database into memory as a Map
map (\x -> memdb ! x) values
map
的每次迭代中生成一次。 .我可以使用这样的东西来做到这一点:
make_memdb = -- equivalent to memdb in previous example
memdb <- make_memdb
map (\x -> memdb ! x) values
memdb
在每个使用它的功能中。有什么办法可以:
memdb
每次它被称为 OR
make_memdb
中产生的值作为一个常数,这样我就可以避免将它传递给使用它的每个函数?
最佳答案
由于您的 map 来自数据库,这意味着它不能是常数,因为它在您的应用程序运行之间可能会有所不同。
But this would mean that I would have to pass memdb in to every function that uses it.
main = do
memdb <- make_memdb -- Get the memdb from the database once and for all
runReaderT foo memdb
foo = do
memdb <- ask -- Grab the memdb. Will not reload from the database
liftIO $ putStrLn "Hello, world" -- IO actions have to be lifted
-- [...]
关于haskell - 有没有办法在 Haskell 中记住一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7210510/
我是一名优秀的程序员,十分优秀!