- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
了解 unsafePerformIO
(如何使用它以及何时使用它),我已经实现了一个用于生成唯一值的模块。
这是我所拥有的:
module Unique (newUnique) where
import Data.IORef
import System.IO.Unsafe (unsafePerformIO)
-- Type to represent a unique thing.
-- Show is derived just for testing purposes.
newtype Unique = U Integer
deriving Show
-- I believe this is the Haskell'98 derived instance, but
-- I want to be explicit, since its Eq instance is the most
-- important part of Unique.
instance Eq Unique where
(U x) == (U y) = x == y
counter :: IORef Integer
counter = unsafePerformIO $ newIORef 0
updateCounter :: IO ()
updateCounter = do
x <- readIORef counter
writeIORef counter (x+1)
readCounter :: IO Integer
readCounter = readIORef counter
newUnique' :: IO Unique
newUnique' = do { x <- readIORef counter
; writeIORef counter (x+1)
; return $ U x }
newUnique :: () -> Unique
newUnique () = unsafePerformIO newUnique'
Data.Unique
选择了和我一样的数据类型;另一方面,他们选择了
newUnique :: IO Unique
类型。 ,但我想远离
IO
如果可能的话。
最佳答案
对待 unsafePerformIO
作为对编译器的 promise 。它说“我保证您可以将此 IO 操作视为纯值,并且不会出错”。它很有用,因为有时您可以为使用不纯操作实现的计算构建纯接口(interface),但编译器无法验证何时是这种情况;而是 unsafePerformIO
允许你 Handlebars 放在心上,发誓你已经验证了不纯的计算实际上是纯的,所以编译器可以简单地相信它是纯的。
在这种情况下, promise 是错误的。如果 newUnique
那时是一个纯函数let x = newUnique () in (x, x)
和 (newUnique (), newUnique ())
将是等效的表达式。但是你会希望这两个表达式有不同的结果;一对相同 Unique
的副本一种情况下的值,以及一对两个不同的Unique
另一方的值(value)观。使用您的代码,真的无法说出这两个表达式的含义。它们只能通过考虑程序在运行时执行的实际操作顺序来理解,而对它的控制正是您在使用 unsafePerformIO
时放弃的内容。 . unsafePerformIO
表示是否将任一表达式编译为 newUnique
的一次执行并不重要或者两个,并且 Haskell 的任何实现在每次遇到此类代码时都可以自由选择它喜欢的任何内容。
关于haskell - 我在滥用 unsafePerformIO 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19371636/
NSNotificationCenter 真的很酷,我们可以用它构建非常可扩展的应用程序。我的问题是:我们是否可以在应用程序中滥用 NSNotificationCenter,即密集使用它,还是它不是一
据我了解,标签是“ anchor ”标签,它有三个用途: 1) 描述并命名页面上的项目 2) 单击时将用户转到另一个页面 Yahoo! 3) 单击时将用户发送到同一页面中的另一个位置 Foo! 有人
我有一个使用 OpenMP 并行化 for 循环的程序。在循环内部,线程将写入共享变量,因此我需要同步它们。但是,我有时会遇到段错误或双重释放或损坏错误。任何人都知道会发生什么?感谢致敬!这是代码:
我很确定过去曾有开发人员严重滥用了我们可怜的 ASP.NET session 状态。 Session 很想帮助跟踪请求之间的事情,但只能做这么多!请帮我减轻它的痛苦,让它和我们一起快乐地工作。 有谁知
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 4年前关闭。 Improve this
Git 本质上是事件存储的实现,其中存储的数据是目录结构中的文件。已知可以可靠地解决问题: 存储更改历史记录 向客户端传输最少的数据以获取最新数据 可以回滚到之前的状态 可以通过在 Git 上编写包装
我有两项服务-Lincr和LinkBunch。 Lincr是简单的简捷URL缩短服务,而LinkBunch可让您将多个链接缩短为一个链接。我在服务中张贴了太多垃圾邮件,因此不得不关闭Lincr。现在,
我必须接管一个包含多个层、服务和组件的巨型整体。 当我浏览代码时,我首先意识到的是 MDC 的使用. 一些例子: public void setContextOrderId(String orderI
我正在尝试制作一种从外部文件中提取电子邮件信息并将其存储在字符串中的方法。我正在使用 .get 来提取字符,但我的调试器说:没有重载函数的实例与参数列表匹配。它还说它不能将第三个参数从 const c
我的 UI 中需要一个标签来容纳最多 32 个字符而不变形,因此我启用了自动收缩并将行数更改为 0: 这是我在 IB 上的标签: 现在,当我运行应用程序时,它会在屏幕外运行: 我怎样才能使它相应地适应
我有一个整数列表(当前),我想检查这个列表是否包含预期列表中的所有元素,甚至不包含列表 notExpected 中的一个元素,所以代码如下: List expected= new ArrayL
目前正在阅读 Bloch 的Effective Java(第 2 版),他以粗体字指出,在 Web 应用程序中过度使用 POST 本质上是不好的。不幸的是,他没有具体说明原因。 这让我大吃一惊,因为当
提前为第一个可能很愚蠢的帖子道歉。虽然有很多关于这个主题的 Material ,但其中很少有是确定的和/或对我来说可以理解的。 我有一个 AlignedArray 模板类,可以在堆上以任意对齐方式动态
MSDN 建议 RegisterWindowMessage() 函数仅用于注册要在进程之间发送的消息。如果需要在一个进程内发送消息,可以从 WM_APP 到 0xBFFF 范围内安全地选择它。 但是在
我已经阅读了很多关于在每个 SELECT 上使用许多 JOIN 语句的关系数据库。但是,我一直想知道滥用这种方法从长远来看是否会出现任何性能问题。 例如,假设我们有一个users 表。我通常会添加“最
所以我有一些使用保留字 property 的遗留代码,嗯,错了。在继承的基类中,它们基本上已实现。 class TestClass(object): def __init__(self, pr
出于方便和安全的原因,我想使用 using 语句从池中分配和释放对象 public class Resource : IDisposable { public void Dispose()
您很可能已经知道,在 JQuery 中选择文档中具有特定 CSS 类的所有元素,然后使用链接将公共(public)事件处理程序分配给所选元素很简单: $(".toolWindow").click(to
我需要解析一个 xml 文件,它实际上是一个非常大的树结构的图像,所以我使用 XmlReader 类来“动态”填充树。每个节点仅通过 ReadSubtree() 函数传递它期望从其父节点获得的 xml
我想知道这些指南的用途: 1 - 我多久可以读取 NSUserDefaults 2 - 我可以在 NSUserDefaults 中合理存储多少数据 显然,NSUserDefaults 的使用量是有限制
我是一名优秀的程序员,十分优秀!