Horwett Horwett - 6 months ago 72
Scala Question

Scala. How to delete all n'th element from Stream

Now studying Streams in Scala. Can anybody help me with function that will delete all n'th element from Stream.
[2,3,99,1,66,3,4];3 must return this: [2,3,1,66,4]

Answer Source
myStream.zipWithIndex                    //attach index to every element
        .filter(x => (1 + x._2) % n > 0) //adjust index, remove every nth
        .map(_._1)                       //remove index

Oops, almost forgot: filter and map can be combined.

        .collect{case (e,x) if (1 + x) % n > 0 => e}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download