scala / scala-library-next

backwards-binary-compatible Scala standard library additions
Apache License 2.0
69 stars 17 forks source link

Combinator to create IndexedSeq backed by a size and a user-supplied function #103

Open benhutchison opened 2 years ago

benhutchison commented 2 years ago

It appears we lack a combinator to create IndexedSeq[A] backed by a size and a user-supplied function Int => A, eg

def fromFunction[A](n: Int, f: Int => A) = new IndexedSeq[A] {
  def length = n
  def apply(i: Int): A = f(i)
}

The difference to the existing tabulate is that tabulate will eagerly invoke the function at creation time, whereas the above impl will invoke f when needed.