How do you turn a Scala list into pairs?

前端 未结 3 1108
孤城傲影
孤城傲影 2020-12-18 18:09

I am trying to split Scala list like List(1,2,3,4) into pairs (1,2) (2,3) (3,4), what\'s a simple way to do this?

3条回答
  •  余生分开走
    2020-12-18 19:08

    To produce a list of pairs (i.e. tuples) try this

    List(1,2,3,4,5).sliding(2).collect{case List(a,b) => (a,b)}.toList
    

提交回复
热议问题