https://kotlinlang.org logo
Title
b

bj0

12/19/2017, 10:05 PM
is there a way to create a single element
Iterable
without creating an array or list?
a

adam-mcneilly

12/19/2017, 10:05 PM
What's the benefit of this?
t

Travis

12/19/2017, 10:05 PM
Same, curious, to what end?
listOf(element)
would be iterable, but it builds a list…
a

adam-mcneilly

12/19/2017, 10:07 PM
^ which is what I wanted to suggest, but I don't understand what blocker the list causes
b

bj0

12/19/2017, 10:07 PM
i'm using a
when
condition inside a
flatMap
, most of the conditions return lists but one has a single element, I was wondering if there was something without the (probably very low) overhead of creating an unnecessary list/array
it's not a blocker, It just seems unnecessary
so I was wondering if there was something similar to
.just()
from Rx
a

adam-mcneilly

12/19/2017, 10:09 PM
What's the ultimate return type here? If all cases return a list, I would expect the calling code to expect a list to be returned, but if there's a case for only one then your calling code might get ugly? Side note: Loosely wondering if a sealed class could work here, but don't know if there's enough context.
t

Travis

12/19/2017, 10:10 PM
the analog appears to be that,
listOf(element)
- I mean,
Observable.just
still creates an Observable…
b

bj0

12/19/2017, 10:10 PM
flatMap
expects an
Iterable
t

Travis

12/19/2017, 10:10 PM
Yeah, gonna need more context here
a

adam-mcneilly

12/19/2017, 10:11 PM
it's a good question, and I'm curious to see if someone thinks of anything, but I wouldn't argue that the list creation is too much overhead to stress if you don't get an answer. ¯\_(ツ)_/¯
b

bj0

12/19/2017, 10:11 PM
yea I didn't know if there was a lightweight wrapper around a value that implemented
Iterable
, but I don't see anything like that
It's not a lot, like I said, I was just curious, I'm fine using listOf
👍 1
but the context is replacing some bytes with multiple bytes in a bytestream, hence the
flatMap
, and
flatMap
requires an
Iterable<T>
p

paulblessing

12/19/2017, 10:12 PM
listOf()
with a single item ultimately creates a
java.util.SingletonList
(on the JVM at least) which is a very lightweight list implementation.
👍 3
b

bj0

12/19/2017, 10:13 PM
good to know