summaryrefslogtreecommitdiff
path: root/src/utils/StreamUtils.scala
blob: cb296d3adb4d6f66ffbe8f0fb3ca5d9452a47ec9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package ixee.cryptopals.utils

object StreamUtils {
  implicit class RichStream[A](x: Stream[A]) {
    def splice(other: Stream[A]): Stream[A] = {
      var i = 0
      val selfIter = x.iterator
      val otherIter = other.iterator
      Stream.continually {
        i = i + 1
        if (i % 2 == 0) {
          otherIter.next
        } else {
          selfIter.next
        }
      }
    }
    def spliceEvery(n: Int)(other: Stream[A]): Stream[A] = {
      var i = 0
      val selfIter = x.iterator
      val otherIter = other.iterator
      Stream.continually {
        i = i + 1
        if (i % (n + 1) == 0) {
          otherIter.next
        } else {
          selfIter.next
        }
      }
    }
  }
}