问题标签 [ioref]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
147 浏览

haskell - 如果发生错误,IORef 保持值

如果内部发生错误,有没有办法不修改 IORef 值modifyIORef

0 投票
1 回答
270 浏览

haskell - IORef 处理列表

这是我之前提出的问题的后续。我想知道IORefO(1)每次调用fetch. 我怀疑这是因为IORef可能只是保留一个指向列表头的指针(而不是遍历和复制整个列表,每次都是 O(n)。只需将指向新头的指针更改为 O(1),并且应该防止对整个列表的急切评估)。但是,ghc-core不会显示低级代码。所以,在这里问:

0 投票
1 回答
472 浏览

performance - IORef 和 STRef 的编译

为了测量这些 Refs 的性能,我将 GHC 生成的程序集转储到以下代码中:

我希望 IORef 被完全优化掉,只留下一个系统调用来用字符串“18”编写标准输出。相反,我得到了 250 条装配线。你知道有多少人会被处决吗?这是我认为该程序的核心:

我很担心这个jmp stg_newMutVar#。它不在程序集中的其他位置,因此 GHC 可能会在稍后的链接阶段解决它。但为什么它甚至在这里,它有什么作用?我可以在没有任何未解析的 haskell 符号的情况下转储最终程序集吗?

0 投票
4 回答
362 浏览

haskell - atomicModifyIORef 的额外结果参数的目的是什么?

的签名modifyIORef很简单:

不幸的是,这不是线程安全的。有一个替代方案可以解决这个问题:

这两个功能之间究竟有什么区别?b在修改IORef可能从另一个线程读取的参数时,我应该如何使用该参数?

0 投票
1 回答
221 浏览

haskell - IORef still refers to the old value after update

Background

I am a Schemer starting to learn Haskell. I am trying to implement a Scheme interpreter in C, following chapter 4 of SICP. It turns out programming directly in C is too hard. So I decide to first prototype in Haskell. With the help of Write Yourself a Scheme in 48 Hours, I have implemented everything except variables, closures and environment.

Problem

The modification to IORef doesn't persist between invocations of main. I expect the program to print (False) (True) (True) (True)... but in fact it prints (False) (True) (False) (True) (False) (True)...

Strip-down version of the code:

Syntax-highlighted version: ioref.hs

Thanks for your help!

0 投票
1 回答
292 浏览

haskell - 使用 IO 时如何在 Haskell 中的两个函数调用之间共享 IORef 状态?

我正在尝试学习 Haskell,并且正在尝试使用 IORef 来保存和查找记录。我的代码看起来像这样(请注意,我在此示例中选择“String”作为 IORef 类型只是为了方便和简洁,在我的实际代码中我使用的是记录。并且还忽略了我使用的是 Set 代替的地图,我会改变它):

我想要实现的是在两个函数调用之间共享状态(Set),以便findStringInState可以返回String我刚刚插入到 Set 中的状态。但是当我运行这个doStuff函数时,我得到了这个:

我可能误解了一些东西,因为我认为 IORef 确实应该是我的状态的容器。

  1. 为什么这不起作用?
  2. 我该怎么做才能让它发挥作用?
0 投票
1 回答
131 浏览

haskell - Zoom instance for ReaderT IORef

I'm new to lens and having trouble implementing the Zoom instance for this type:

I've been trying to make a new IORef with the lens substate, run the ReaderT on that substate, and then grab the changed substate and replace it in the main IORef:

l seems to be different from a normal lens so ^. and .~ doesn't compile with it and I get errors like this:

Can anyone help me get this Zoom instance to work properly? Thanks!

0 投票
1 回答
172 浏览

haskell - 如何重写使用 modifyIORef' 的 Haskell 函数以使用 atomicModifyIORef'

我正在尝试解决 Haskell 为相同参数提供不同输出的问题。有人已经建议这可能是与线程相关的问题。

我设法重写了一个简单的函数来使用原子版本,但更复杂的函数我需要帮助。

这是我的代码:

我的发现

按照建议,我尝试使用 do 表示法重写函数:

使用原子代码和 do 表示法的版本:

0 投票
1 回答
2214 浏览

haskell - Haskell 中的 IORef

我想知道 Haskell 中是否有IORef的合法用法?更具体地说,如果有人可以解决以下问题或指出适当的地方以了解更多信息,我将不胜感激:

  1. 使用 IORef 是否被认为是一种糟糕的 Haskell 实践?如果是,为什么?更具体地说,它比 IO monad 好还是坏?
  2. 如果有人想为程序添加状态,那么 state monad 不是更好(更纯粹)的方式来做到这一点。如果一个人感觉更有必要,他难道不能仍然使用 STM 和 MVar,而且还能过得更好吗?

  3. 是否有使用 IORefs 而不是 STM、MVar 或纯 IO 可以轻松处理的编程场景?

我正在阅读一篇使用 IORef 作为代码片段的论文,由于我对 IORef 的负面看法,我很难阅读它。与其沉溺于我的无知,我认为向我的 Haskeller 同伴寻求帮助可能是一个更好的主意。

0 投票
1 回答
267 浏览

haskell - Haskell IORef - 答案与获得答案的函数

我试图了解如何IORefs真正使用,并且在遵循我在https://www.seas.upenn.edu/~cis194/spring15/lectures/12-unsafe.html上找到的示例代码时遇到了麻烦

printCounts执行“ ”时,为什么在“ ”块c <- newCounter中没有c得到执行工作的结果,这似乎应该在第一次调用时分配给常量“”然后永远不会改变?相反,似乎被分配了在该 " " 块中定义的函数,然后每次到达另一个 " " 时都会重新执行该函数。似乎答案在某种程度上在于具有双重嵌套的“ ”类型,但我无法理解为什么这会使函数在调用时重新执行,而不是只评估一次常量。newCounterreturn $ doIO 0creturn $ doprintCountsprint =<< cnewCounterIO (IO Int)c