summaryrefslogtreecommitdiff
path: root/src/utils/StreamUtils.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/StreamUtils.scala')
-rw-r--r--src/utils/StreamUtils.scala45
1 files changed, 19 insertions, 26 deletions
diff --git a/src/utils/StreamUtils.scala b/src/utils/StreamUtils.scala
index cb296d3..4f803c0 100644
--- a/src/utils/StreamUtils.scala
+++ b/src/utils/StreamUtils.scala
@@ -1,32 +1,25 @@
package ixee.cryptopals.utils
+import ixee.cryptopals.utils.FunctionUtils._
+
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
- }
- }
- }
+ def splice(other: Stream[A]): Stream[A] =
+ spliceEvery(1)(other)
+
+ def spliceEvery(n: Int)(other: Stream[A]): Stream[A] =
+ x.grouped(n).zip(other.iterator).flatMap(tup(_ :+ _)).to[Stream]
+
+ def continually: Stream[A] =
+ x append x.continually
}
+
+ def continuous[A](xs: Seq[A]): Stream[A] =
+ xs.to[Stream] append continuous(xs.to[Stream])
+
+/*
+ // Or just xs.to[Stream] ...
+ def fromSeq[A](xs: Seq[A]): Stream[A] =
+ xs.foldLeft(Stream.empty[A])(_ :+ _)
+*/
}