Hadi Elmougy Hadi Elmougy - 2 months ago 18
Scala Question

partition an Array with offset

in Clojure I can partition a vector with offset step like

(partition 2 1 [1 2 3 4])


this returns a sequence of lists of n items each at offsets step apart.
for example the previous method returns

((1 2) (2 3) (3 4))


I just wonder how can I acheive the same in Scala

Answer

I think this does what you need:

List(1,2,3,4).sliding(2,1).toList