I'm a bit stuck on traversals. I've had a good loo...
# arrow
p
I'm a bit stuck on traversals. I've had a good look at the documentation but it's not clear to me. I have a simple use case, a list of of lists, where I want to update a couple of properties like so. Does anyone know how do do this ?
Copy code
@optics
data class MyEmployee(val name: String, val age: Int) {
    companion object
}
@optics
data class MyCompany(val employees: List<MyEmployee>) {
    companion object
}

fun main() {

 val companies = listOf(
        MyCompany(listOf(MyEmployee("John", 30), MyEmployee("Jane", 25))),
        MyCompany(listOf(MyEmployee("Alice", 35), MyEmployee("Bob", 40)))
    )

    // update the age and name of each employee in each company
}
Copy code
Every.list<MyCompany>().employees.every.age.modify(companies) {...} //or set()
something like that.
p
Yes I've seen that in the documentation, but struggling for a nested list (I tried your example and it doesn't compile)
d
You have to check out the generated code, you'll probably see the extension for employees, then you can take it from there... you're right the docs are a bit lacking... so looking a bit at the sources is sometimes helpful.
👍 1
p
Unfortunately the generated code didn't help me (tl;dr I didn't understand it). If anyone has any ideas I'd be very grateful.
d
Maybe this:
Copy code
Every.list<MyCompany>().employees.every(Every.list()).age.modify(companies) {...}
The problem is that I made my own lenses in those cases, so I don't have the same setup as you... so I'm not sure about the previous. It should work...
Did it work @Paul N?