sindrenm
05/03/2023, 3:03 PM@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:
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
<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.