简体   繁体   English

scala:foldLeft与zipWithIndex

[英]scala: foldLeft with zipWithIndex

The following code works : 以下代码有效

likertRoundDfSeq:Seq[DataFrame] =    ......
likertRoundDfSeq match
  {

    case head :: tail => tail.foldLeft(head){(dforg,df1)=>
      DataFrameUtils.join(dforg,devianceFromAverageOneRound(df1),"A_RowId")
    }
  }

BUT, I need to add an index as an additional argument to devianceFromAverageOneRound 但是,我需要将索引添加为devianceFromAverageOneRound的附加参数

I thought of doing this with zipWithIndex Perhaps, like this: 我想用zipWithIndex做到这一点,也许是这样的:

 likertRoundDfSeq match
  {

    case head :: tail => tail.zipWithIndex.foldLeft(head){(dforg,df1)=>
      DataFrameUtils.join(dforg,devianceFromAverageOneRound(df1,*myzipindex*),"A_RowId" )
    }
  }

But I am not sure how to break out the dataframe and idx in this case. 但是我不确定在这种情况下如何分解数据帧和idx。 Intellij does not seem to guide me on this, so I'm a bit lost Intellij似乎不指导我,所以我有点迷路

Any advice would be appreciated 任何意见,将不胜感激

The tail of your DF Seq is now a list of Tuple2[DataFrame, Long], hence your foldLeft should look like the following: 现在,DF Seq的尾部是Tuple2 [DataFrame,Long]的列表,因此, foldLeft应该如下所示:

case head :: tail => tail.zipWithIndex.foldLeft(head){ (dforg, df1) =>
  DataFrameUtils.join(dforg, devianceFromAverageOneRound(df1._1, df1._2), "A_RowId")

This assumes your new devianceFromAverageOneRound(DataFrame, Long) still returns a DataFrame (and not Tuple2[DataFrame, Long] ). 假设您的新的devianceFromAverageOneRound(DataFrame, Long)仍返回一个DataFrame (而不是Tuple2[DataFrame, Long] )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM