From 0a3371042ff2f79f99be3ed2ac50be37338bb53c Mon Sep 17 00:00:00 2001 From: iximeow Date: Mon, 24 Nov 2014 01:21:00 -0800 Subject: Add stream utils --- src/utils/StreamUtils.scala | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/utils/StreamUtils.scala (limited to 'src') 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 + } + } + } + } +} -- cgit v1.1