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] = 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]) def pairsOf[A](xs: Seq[A]): Seq[(A, A)] = (xs.init zip xs.tail.tails.toSeq) flatMap { case (elem, rest) => rest zip Stream.continually(elem) } /* // Or just xs.to[Stream] ... def fromSeq[A](xs: Seq[A]): Stream[A] = xs.foldLeft(Stream.empty[A])(_ :+ _) */ }