How do you rotate (circular shift) of a Scala collection

后端 未结 11 1503
盖世英雄少女心
盖世英雄少女心 2020-12-19 03:40

I can do this quite easily, and cleanly, using a for loop. For instance, if I wanted to traverse a Seq from every element back to itself I would do the

11条回答
  •  隐瞒了意图╮
    2020-12-19 03:55

    Is it like below:

    scala> def rotatedView(i:Int)=Seq(1,2,3,4,5).drop(i)++Seq(1,2,3,4,5).take(i)
    rotatedView: (i: Int)Seq[Int]
    
    scala> rotatedView(1)
    res48: Seq[Int] = List(2, 3, 4, 5, 1)
    
    scala> rotatedView(2)
    res49: Seq[Int] = List(3, 4, 5, 1, 2)
    

提交回复
热议问题