https://kotlinlang.org logo
Title
a

Alec Muffett

10/30/2020, 7:12 AM
Hi All! So
SequenceInputStream()
takes a Java
Enumeration
as an argument, but at the moment I only have a `List<InputStream>`; there's a workaround for this described at https://www.pushing-pixels.org/2018/08/15/converting-list-to-enumeration-in-kotlin.html to provide a
List<T>.toEnumeration()
method, but I am wondering if there's something more elegant?
v

Vampire

10/30/2020, 10:03 AM
More elegant, but less performant as it will create n-1
Vector
instances:
inList.reduce { left, right -> SequenceInputStream(left, right) }
Another way would be
SequenceInputStream(Vector(inList).elements())
❤️ 1
a

Alec Muffett

10/30/2020, 4:12 PM
Thanks, @Vampire
Looks like this might be an RFE?
v

Vampire

10/30/2020, 4:34 PM
An
asEnumeration()
method? Maybe
n

nanodeath

10/30/2020, 9:10 PM
I made this:
private operator fun InputStream.plus(inputStream: InputStream): InputStream = SequenceInputStream(this, inputStream)
might be what you want
v

Vampire

10/31/2020, 10:46 AM
Well, that's neat and probably enables
inList.sum()
, but it is basically the same as my reduce call and thus will create n-1 new
Vector
instances.
n

nanodeath

11/02/2020, 4:30 PM
it doesn't create any
Vector
instances though
v

Vampire

11/02/2020, 5:17 PM
Sure it does, look inside the
SequenceInputStream
constructor you are calling 😉
n

nanodeath

11/02/2020, 5:18 PM
and it does indeed use a Vector
v

Vampire

11/02/2020, 6:06 PM
The first link you gave was the implementation of GNU classpath which I was not even aware of. 😄
I just navigated to the constructor from my IDE
Of course this is an implementation detail and could be different in different implementations as you just have proved. 🙂
n

nanodeath

11/02/2020, 6:27 PM
I feel like they only use Vector so they can keep using Enumeration internally 😕 rather than porting everything over to...idk, Iterable
v

Vampire

11/02/2020, 6:36 PM
Yes, seems so.
SequenceInputStream
is simply very old and pre-collections framework. I wonder they didn't add a collections variant.
n

nanodeath

11/02/2020, 6:38 PM
no doubt Guava has something similar/better
v

Vampire

11/02/2020, 6:44 PM
Never used Guava. Isn't it about collections, not about IO streams?
n

nanodeath

11/02/2020, 6:46 PM
yes, but it also has kinda everything. I'm looking