scala - 仅调用一次方法并应用过滤器将结果保存在scala中的不同变量中
问题描述
temp1 <- Future.sequence(file.map { ref =>
readFile(ref, config).map { (ref, _) }
}).map(f => f.filter(parsed => parsed._2.errors.nonEmpty))
temp2 <- Future.sequence(file.map { ref =>
readFile(ref, config).map { (ref, _) }
})
我不想调用 readFile 方法 2 次。如何只调用一次并根据过滤器将输出保存在 temp1 和 temp2 中。
解决方案
如果我正确理解了您的代码,您想要temp1
并temp2
包含类似的数据,但是在temp2
您另外调用map(...)
时,为什么不能temp2
先创建然后设置temp1 = temp2.map(f => f.filter(parsed => parsed._2.errors.nonEmpty))
这种方式,您只需调用一次 readFile。还是我错过了什么?
推荐阅读
- azure-devops - 以编程方式访问 Azure Devops 中的管道变量组
- sql - SQL Select:匹配id的行是否都具有相同的列值
- testing - 对哪种类型的测试感到困惑——理智、烟雾或回归
- java - 主线程在 CompletableFuture 完成之前退出
- reactjs - React - 取消注册已注册的服务人员
- php - PHP Laravel 多次返回
- python-3.x - pandas - 在具有不同位置的不同字符集之间提取数字
- xaml - 如何使用 Grid 并创建它?
- javascript - Puppeteer 在当前窗口中启动一个新选项卡,而不是新窗口
- bash - 由于包含引号的字符串变量,bash 脚本中的 curl 命令问题