scala-library-next icon indicating copy to clipboard operation
scala-library-next copied to clipboard

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

Open benhutchison opened this issue 3 years ago • 0 comments

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.

benhutchison avatar Nov 25 '21 06:11 benhutchison