summaryrefslogtreecommitdiff
path: root/src/utils/StreamUtils.scala
blob: be5246c9eb87cd1522089f59465fa2991a4cc912 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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])(_ :+ _)
*/
}