- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
试图了解如何最好地处理 FP 中的副作用。
我实现了这个基本的 IO 实现:
trait IO[A] {
def run: A
}
object IO {
def unit[A](a: => A): IO[A] = new IO[A] { def run = a }
def loadFile(fileResourcePath: String) = IO.unit[List[String]]{
Source.fromResource(fileResourcePath).getLines.toList }
def printMessage(message: String) = IO.unit[Unit]{ println(message) }
def readLine(message:String) = IO.unit[String]{ StdIn.readLine() }
}
- load lines from log file
- parse each line to BusinessType object
- process each BusinessType object
- print process result
val load: String => List[String]
val parse: List[String] => List[BusinessType]
val process: List[BusinessType] => String
val output: String => Unit
val load: String => IO[List[String]]
val parse: IO[List[String]] => List[BusinessType]
val process: List[BusinessType] => IO[Unit]
val output: IO[Unit] => Unit
- Aren't case 1 and 2 really the same thing?
- In case 2 aren't we just delaying the inevitable?
as the parse function will need to call the io.run
method and cause a side-effect?
- when they say "leave side-effects until the end of the world"
how does this apply to the example above? where is the
end of the world here?
最佳答案
您的 IO monad 似乎缺少所有 monad 的东西,即您可以使用的部分 flatMap
在它之上以较小的 IO 构建更大的 IO。这样,一切都保持“纯净”,直到调用 run
在最后。
In case 2 aren't we just delaying the inevitable? as the parse function will need call the io.run method and cause a side effect?
parse
函数不应调用
io.run
.它应该返回另一个 IO,然后您可以将其与其输入 IO 结合。
when they say "leave side-effects until the end of the world" how does this apply to the example above? where is the end of the world here?
run
一次。程序的其余部分“纯粹”为此构建了一个巨大的 IO。
def load(): IO[Seq[String]]
def parse(data: Seq[String]): IO[Parsed] // returns IO, because has side-effects
def pureComputation(data: Parsed): Result // no side-effects, no need to use I/O
def output(data: Result): IO[Unit]
// combining effects is "pure", so the whole thing
// can be a `val` (or a `def` if it takes some input params)
val program: IO[Unit] = for {
data <- load() // use <- to "map" over IO
parsed <- parse()
result = pureComputation(parsed) // = instead of <-, no I/O here
_ <- output(result)
} yield ()
// only `run` at the end produces any effects
def main() {
program.run()
}
关于scala - 处理 FP、IO Monad 中不纯的副作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56169426/
什么时候使用 fp:strict 而不是 fp:precise?如果我想要“更精确”的计算并避免舍入误差,使用前者是否更好?使用这两者背后的启发是什么? 最佳答案 标准 IEEE 754 指定了一种用
我检测到发布版本和调试版本之间的程序结果存在一些差异。经过一些研究,我意识到一些浮点优化导致了这些差异。我已经通过使用 fenv_access pragma 禁用一些关键方法的优化来解决了这个问题。
我在 Built-in Functions — Python 3.7.0 documentation 中阅读了内置函数 iter 的示例 with open('mydata.txt') as fp:
我在 Built-in Functions — Python 3.7.0 documentation 中阅读了内置函数 iter 的示例 with open('mydata.txt') as fp:
我一直在查找一些用 C 语言读取文件的例子,我看到了这两个例子 fgets(buff,255,(FILE*)fp); 和 fgets(buff,255,fp); 假设 fp 是前面定义的文件指针 "F
我是 FP-TS 的新手,但仍然不太明白如何使用 TaskEither .我正在尝试异步读取文件,然后使用 yaml-parse-promise 解析结果字符串。 ==编辑== 我用文件的完整内容更新
我需要在我的 Mac 上编译一个 C 文件,该文件是在仅假设 Linux 和 GCC 环境下编写的。该文件包含一行 if(fp->_IO_write_base == fp->_IO_write_end
for line in fp 和 for line in fp.readlines() 有什么区别? with open(filename, 'r') as fp : for line in
这可能非常无聊,但谷歌搜索确实没有帮助。 在python官方文档中,它经常将文件称为fp: with open(filename, "w") as fp: fp.write() “p”代表什么? 最佳
我想知道如果我设置/fp:fast 而不是 fp:precise 会遇到什么样的错误?我在 MSV10 下工作 我对最大位数为 8 的 double 执行/,*,+,- 运算,例如 1.4379294
我正在使用 Groovy 进行一个项目,我想采用一个员工数组,这样在数组中没有经理跟随他们的下属。原因是我需要将人员添加到数据库中,我不希望分两次完成。 所以,我基本上有: 12
背景: 许多年前,我继承了一个代码库,该代码库使用 Visual Studio (VC++) 标志“/fp:fast”在特定的计算量大的库中生成更快的代码。不幸的是,'/fp:fast' 产生的结果与
我正在尝试从PostgreSQL数据库检索数据,将其转换为视图模型数组,并将数据返回给客户端,在本例中最好是作为单个对象,而不是数组。对于我在错误消息下面列出的代码,我收到以下错误。我是函数式编程领域
所以我尝试将此类 Matlab 代码转换为 C++: ss = 'file.mask' fp = fopen(ss, 'rb'); sx = fread(fp, 1, 'int32') sy = f
我是fp-ts的新手,请帮助我解决问题: 我需要使用异步功能在不同级别上多次记录同一错误。这是我的示例代码: const myProgram = pipe( tryCatch(() => so
我正在尝试在为浏览器编写的库中使用 lodash/fp。我正在使用 Webpack 来打包我的库。 在我的代码中,我当前加载 lodash/fp 如下(ES2015 样式。我使用 Babel 对其进行
我的问题是引用 this post ,具体来说: data Actions a = Actions { actEval :: a, actMap :: (a -> a) -> Act
我正在阅读 FP,我有两个基本问题: FP 表示函数应该接受一个输入并给出单个输出。那么我该怎么办void方法?它不返回任何东西吗? FP 说函数应该是单一的 责任,那我们怎么处理log方法内的语句?
我是 c 初学者,所以我在让用户输入姓氏、逗号和名字时遇到问题。但是它将传递给函数调用 int get_name(FILE *fp) 在我的主要功能中。如果我必须使用参数参数,我就会遇到问题。 示例,
我有一些这样的序列 (100) - (102) - (103) - (104,106) - (108) (101) - (103) (102) - (106) 在 C++ 中有一些有效的实现前缀树或
我是一名优秀的程序员,十分优秀!