lensesarrow-kt

How do you add/remove an element of immutable list using Arrow Optics?


I have a complicated, immutable data structure that includes simple fields, but also maps and lists in the hierarchy. Maybe I'm just not reading the documentation closely enough, but there doesn't seem to be an easy way to modify the list as a whole without doing some pretty boiler-platey stuff.

For example, say I had foo.bar.list and I wanted to add an element at index i to the list. The only way I see to do that is to use the getter to get the current list, do something like

list.subList(0, i) + listOf(newElement) + list.subList(i, list.size)

and pass that to the setter.

Is there something like list.add(index, element) or list.remove(index) that you can call inside a lens to modify just the list part and keep the rest of the structure the same.

Or is there some easy way to do this with the At, Index, or Traversal parts of the Collections DSL that I just don't see?


Solution

  • This is possible with Arrow Optics, and Index as you've indicated. Here is a full example,

    import arrow.optics.dsl.index
    import arrow.optics.optics
    import arrow.optics.typeclasses.Index
    
    @optics data class Foo(val bar: Bar) {
        companion object
    }
    @optics data class Bar(val list: List<Int>) {
      companion object
    }
    
    val foo = Foo(Bar(listOf(1, 2, 3)))
    
    fun main() {
      Foo.bar.list.index(Index.list(), 1).set(foo, 5)
        .let(::println) // Foo(bar=Bar(list=[1, 5, 3]))
    }
    

    You can of course also use the other operators of Optics. This example was written with Kotlin 1.6.21, id("com.google.devtools.ksp") version "1.6.21-1.0.6" and Arrow 1.1.3.

    You can of course also handwrite the optics if you prefer not using Google KSP.

    import arrow.optics.Lens
    import arrow.optics.typeclasses.Index
    
    data class Foo(val bar: Bar)
    
    data class Bar(val list: List<Int>)
    
    val bar: Lens<Foo, Bar> = Lens(Foo::bar) { foo, bar -> foo.copy(bar = bar) }
    val list: Lens<Bar, List<Int>> = Lens(Bar::list) { bar, list -> bar.copy(list = list) }
    
    val foo = Foo(Bar(listOf(1, 2, 3)))
    
    fun main() {
      val optic = (bar compose list compose Index.list<Int>().index(1))
      val result = optic.modify(foo) { it + 3 }
      println(result)
    }