仅调用方法一次并应用过滤器以将结果保存在 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。还是我错过了什么?

最新更新