<#C0B8MA7FA|getting-started> - I am new to Kotlin ...
# getting-started
a
#getting-started - I am new to Kotlin and trying to absorb as much as possible. I have a requirement where I have a Multi-Level List that I need to parse up to 4 levels. List<List<List<List<String>>>>. The typical way would be using 4 for loops and keep iterating, but I am looking for a smarter Kotlin way of doing it. Could you guys please help me get there?
Copy code
for(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:
Copy code
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
)
🙂 1
c
Definitely a gross data structure, but you can call
.flatten()
multiple times to reduce the list down to a single
List<String>
Copy code
val deeplyNestedList: List<List<List<List<String>>>> = TODO()
val flatList: List<String> = deeplyNestedList
    .flatten()
    .flatten()
    .flatten()
e
I'd go a bit further and say that if you're iterating, you should do so over the sequence,
Copy code
for (x in deeplyNestedList.asSequence().flatten().flatten().flatten()) {
    println(x)
}
which avoids constructing all the partially-flattened lists along the way
a
@Casey Brooks, can't agree more! Worst DS till date. My Bad! @Casey Brooks & @ephemient Structure is of complex nature - data class object. Parsed via JSON API call:
Copy code
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
)
Will update the query too. Any suggestion for this type of structure?
e
Copy code
for ((string1, string2) in listOfA.asSequence().flatMap { it.b }.flatMap { it.c }.flatMap { it.d }) // ...
a
@ephemient, tried it but getting the following error:
Copy code
Type 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?
e
where is the nullable coming from, you don't have that in your question
but Kotlin has an
.orEmpty()
extension on collections
a
data classes definitions are defined with type ?
Yes, in vanilla for() I used: for(a in As.orEmpty)
e
well you can do that in
flatMap
too
a
Yes, still got the same error.
e
make a real example, then. because what I said does work. https://pl.kotl.in/DQ9EDl_js
a
do you mind checking the URL you shared, I added a piece right below it.
e
changes are not shared. you need to create another URL
a
@ephemient, shared a link. Let me know if it makes sense.