简体   繁体   English

在 F# 中制作纯 printfn/IO

[英]Making printfn/IO pure in F#

There doesn't seem to be much online on making sure functions remains pure in F#.网上似乎没有太多关于确保 F# 中的功能保持纯正的内容。

To create an example, is there any way to make printfn/IO pure in F#?举个例子,有什么办法可以让 F# 中的 printfn/IO 纯?

you can google for F# implementations of Haskell's IO monad.你可以用谷歌搜索 Haskell 的 IO monad 的 F# 实现。 Here 'sa nice example.是一个很好的例子。

Printing to the Console is per definition something that changes state (console state) and do have side-effects (like deleting some of the console buffer).根据定义,打印到控制台会改变 state(控制台状态)并且确实有副作用(例如删除一些控制台缓冲区)。

If you are using somekind of CodeContracts or the like you should consider writing out the function result somewhere else, or using an assertion ( Contract.Assume or Contract.Assert (this is used for static assertions)) for easier confirmation that you code works.如果您正在使用某种 CodeContracts 或类似的东西,您应该考虑在其他地方写出 function 结果,或使用断言( Contract.AssumeContract.Assert (这用于 static 断言)),以便更轻松地确认您的代码是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM