From 8b3cc4835c4c77df9f207bd342ffd024f0f726e7 Mon Sep 17 00:00:00 2001 From: iximeow Date: Wed, 26 Nov 2014 00:41:34 -0800 Subject: Rest of challenge 6 also hacking junk around the frequency maps. --- src/utils/StreamUtils.scala | 45 +++++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 26 deletions(-) (limited to 'src/utils/StreamUtils.scala') 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])(_ :+ _) +*/ } -- cgit v1.1