Ankush
05/26/2022, 6:19 PMfor(a in As){
for(b in Bs){
for(c in Cs){
for(d in Ds){
}
}
}
}
Another train of thought I have is to use flatMap() to flatten the last loop and drop the loops from 4 to 3. But there too, am getting beaten up by Kotlin syntax.
Any help is appreciated! TIA 🙂
EDIT: Sturcture is of complex type in nature:
data class A(
val b: List<B>
)
data class B(
val c: List<C>
)
data class C (
val d: List<D>
)
data class D(
val string1: List<String>,
val string2: String
)
Casey Brooks
05/26/2022, 6:23 PM.flatten()
multiple times to reduce the list down to a single List<String>
val deeplyNestedList: List<List<List<List<String>>>> = TODO()
val flatList: List<String> = deeplyNestedList
.flatten()
.flatten()
.flatten()
ephemient
05/26/2022, 6:24 PMfor (x in deeplyNestedList.asSequence().flatten().flatten().flatten()) {
println(x)
}
which avoids constructing all the partially-flattened lists along the wayAnkush
05/26/2022, 6:33 PMdata class A(
val b: List<B>
)
data class B(
val c: List<C>
)
data class C (
val d: List<D>
)
data class D(
val string1: List<String>,
val string2: String
)
Will update the query too. Any suggestion for this type of structure?ephemient
05/26/2022, 6:36 PMfor ((string1, string2) in listOfA.asSequence().flatMap { it.b }.flatMap { it.c }.flatMap { it.d }) // ...
Ankush
05/26/2022, 7:10 PMType inference failed:
fun <T, R> Sequence<T>.flatMap
(
transform: (T) → Sequence<R>
)
: Sequence<R>
cannot be applied to
receiver: Sequence<A>?
arguments:
(
(A) → List<B>?
)
do you think there is a way out of here? If not, could you point me to more relevant documentation?ephemient
05/26/2022, 7:14 PM.orEmpty()
extension on collectionsAnkush
05/26/2022, 7:16 PMephemient
05/26/2022, 7:17 PMflatMap
tooAnkush
05/26/2022, 7:18 PMephemient
05/26/2022, 7:24 PMAnkush
05/26/2022, 7:37 PMephemient
05/26/2022, 7:39 PMAnkush
05/26/2022, 8:25 PM