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

后端 未结 11 1725
盖世英雄少女心
盖世英雄少女心 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 04:00

    A simple method is to concatenate the sequence with itself and then take the slice that is required:

    (seq ++ seq).slice(start, start + seq.length)
    

    This is just a variant of the drop/take version but perhaps a little clearer.

提交回复
热议问题