Scala.js - How to convert Seq to js.Iterable lazily?

45 views Asked by At

I have a Seq[Byte] that I need to convert to js.Iterable[Byte]. I have some code that does that task:

import scala.scalajs.js
import scalajs.js.JSConverters.iterableOnceConvertible2JSRichIterableOnce

val src: Seq[Byte] = Seq(1, 2, 3)
val target: js.Iterable[Byte] = src.toJSArray

This code works but is likely not very efficient. Can target be created lazily without full array creation via toJSArray?

1

There are 1 answers

0
sjrd On BEST ANSWER

toJSIterable, provided by JSConverters, does exactly what you want:

import scala.scalajs.js
import scala.scalajs.js.JSConverters._

val src: Seq[Byte] = Seq(1, 2, 3)
val target: js.Iterable[Byte] = src.toJSIterable