summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriximeow <me@iximeow.net>2014-11-24 01:21:00 -0800
committeriximeow <me@iximeow.net>2014-11-24 01:21:00 -0800
commit0a3371042ff2f79f99be3ed2ac50be37338bb53c (patch)
tree30a2ff86374091e51fa8f66d6621c49563a2cc4c
parentd32466d20399489ae5cf254588c87e009a9c29f7 (diff)
Add stream 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
+ }
+ }
+ }
+ }
+}