为什么numCapabilities是纯函数? [英] Why is numCapabilities a pure function?
问题描述
在并发库GHC.Conc中有一个叫做 numCapabilities
。它的类型是 numCapabilities :: Int
,它实际上会返回您通过命令行标志传递的一些数字(例如 5
if选项是 + RTS -N 5
)。然而, getArgs
(类型: IO [String]
)基本上是一样的(它返回未解析的非运行时参数),但不是纯函数。
如果唯一的借口是纯代码中经常需要 numCapabilities
,那么不需要其他命令行选项 在纯代码中?
我缺少什么东西或者是 numCapabilities
一个设计缺陷,还是我允许写下面的怪物?
myGetArgs = unsafePerformIO getArgs
我已经看到关于在情况下做什么的非常不同的观点tions喜欢这个。有些人认为编译之间可能有所不同的值不应该是纯粹的,有些人认为只要在程序的本地运行时间内值不发生变化(即在某些配置之后 main ),它应该是纯的。
numCapabilities
在运行时不会改变(据我所知),但 getArgs
。
这是因为有一个 withArgs
函数, getArgs
。所以,这就是答案。
In the concurrency library GHC.Conc there is a function called numCapabilities
. Its type is numCapabilities :: Int
and it actually returns some number you passed by the command line flag (e.g. 5
if the options are +RTS -N 5
).
However, getArgs
(type: IO [String]
) does essentially the same (it returns the unparsed non-runtime arguments) but isn't a pure function.
If the only excuse is that numCapabilities
is often needed in pure code, in what way aren't other command line options not needed in pure code?
Am I something missing or is either numCapabilities
a design flaw or am I allowed to write the following monster?
myGetArgs = unsafePerformIO getArgs
I've seen very varying views on what to do in situations like this. Some think that values that might vary between compiles should not be pure, and some think that as long as a value doesn't change during your program's local run-time (i.e. after some "configuration" has been "set up" in main
), it should be pure.
The base
package seems to have settled on a middle-ground. numCapabilities
will not (as far as I know) change during run-time, but getArgs
might.
This is because there is a withArgs
function that changes the args you get via getArgs
. So, that answers that.
这篇关于为什么numCapabilities是纯函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!