简体   繁体   中英

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

I thought of doing this with zipWithIndex Perhaps, like this:

 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. Intellij does not seem to guide me on this, so I'm a bit lost

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:

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] ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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