有没有办法在纯函数中放置一些不纯净的代码? [英] Is there a way to place some impure code inside pure functions?

查看:68
本文介绍了有没有办法在纯函数中放置一些不纯净的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IO 就像 Maybe 一样,只是 Monad 的一个实例.另一方面,我们拥有用于也许的所有数据构造函数( Just Nothing ),但是没有用于 IO 的构造函数. Reader Writer 也不导出构造函数,它们具有函数,这些函数返回此类型的实例( reader writer ),更重要的是 runReader runWriter ,它们从Monad解开计算结果.

IO, just like Maybe, is just an instance of Monad. On the other hand we have all data constructors for Maybe (Just and Nothing), but no constructors for IO. Reader and Writer do not export constructors too, they have functions, which return instance of this type (reader and writer) and more importantly runReader and runWriter, which unwrap computation result from Monad.

有没有办法解开IO Monad的包装?我想拥有纯粹的功能,可以在后台进行一些不纯的IO计算.目前,我可以在大多数Monad上做到这一点

Is there a way to unwrap IO Monad? I would like to have pure function which do some impure IO computations under the hood. Currently I am able to do this with most of the Monads

我知道此类棘手功能的一个示例: Debug.Trace.trace

I know one example of such tricky function: Debug.Trace.trace

推荐答案

unsafePerformIO :: IO a-> System.IO.Unsafe (基本)中的.

unsafePerformIO :: IO a -> a in System.IO.Unsafe (base).

请谨慎使用并阅读 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆