我可以简化以下zipped和zipWithIndex吗?

时间:2015-08-10 11:01:08

标签: scala

def creditTransfer(invoices: List[Invoice], salons: List[Salon],
                   financialDetails: List[FinancialDetail]) = {
    // TODO: can the following be simplified? instead of having a
    // zipped and a zipWithIndex maybe one zip or something ...
    val creditTxnInforation = (invoices, salons, financialDetails)
    .zipped.toList.zipWithIndex.map {
      case ((invoice, salon, financialDetail), index) =>
        InvoiceToSepaConverter.toCreditTransactionDetail(
           invoice, salon, financialDetail, paymentId(pmtInfId, index))
    }
}

正如评论所解释的那样,我想简化zipped和zipWithIndex。

1 个答案:

答案 0 :(得分:2)

将付款ID转换为可迭代,以便您可以将索引概念推到一边,并一次压缩所有四个迭代。

不确定这是否完全正确,因为我没有足够的代码来编译它,但这些内容是:

def creditTransfer(invoices: List[Invoice], salons: List[Salon],
                   financialDetails: List[FinancialDetail]) = {
  val paymentIds = Stream.from(0).map(paymentId(pmtInfId, _))
  val creditTxnInforation =
    (invoices, salons, financialDetails, paymentIds).zipped
      .map((InvoiceToSepaConverter.toCreditTransactionDetail _).tupled)
}