summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/StreamUtils.scala32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/utils/StreamUtils.scala b/src/utils/StreamUtils.scala
new file mode 100644
index 0000000..cb296d3
--- /dev/null
+++ b/src/utils/StreamUtils.scala
@@ -0,0 +1,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
+ }
+ }
+ }
+ }
+}