Hey, y'all! We're slowly transitioning into using ...
# arrow
s
Hey, y'all! We're slowly transitioning into using Optics (with generated extensions) for a nicer way of dealing with manipulating deeply nested immutable data classes, and I'm loving what it does for me so far. However, I'm getting a little lost when dealing with collections, and I was hoping someone here could guide me in the right direction. I've got something like this:
Copy code
@optics
data class Container(val items: List<Item>) { companion object }

@optics
data class Item(val value: String) { companion object }
I would like to, given a
Container
, replace all of the
items
inside it that matches a condition with another
Item
. Something like:
Copy code
fun main() {
    val container = Container(listOf(Item("foo"), Item("bar"), Item("baz")))
    val newContainer: Container = TODO("""Use optics to convert all Item("bar") with Item("replaced")""")
    assert(newContainer.items[1].value == "replaced")
}
I've been trying to go down the route of
<http://Container.items.at|Container.items.at>(...)
route, but I can't really seem to figure out the
At
APIs. Something like
Copy code
<http://Container.items.at|Container.items.at> { it.id == "bar" }.modify { Item("replaced") }
would make sense to me, but the API's are clearly a little different than that.