首页 > 解决方案 > 仅调用一次方法并应用过滤器将结果保存在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 中。

标签: scala

解决方案


如果我正确理解了您的代码,您想要temp1temp2包含类似的数据,但是在temp2您另外调用map(...)时,为什么不能temp2先创建然后设置temp1 = temp2.map(f => f.filter(parsed => parsed._2.errors.nonEmpty))这种方式,您只需调用一次 readFile。还是我错过了什么?


推荐阅读