Index provides an Optional for a structure
S to focus in optional
A at a given index
If, for a structure
S, the optionally focus
A can be indexed by
Index can create an
Optional with focus at
S for a given index
We can use that
Optional to safely operate on that focus
S (i.e., operating on items in a
List based on the index position).
import arrow.optics.typeclasses.Index val thirdListItemOptional = Index.list<String>().index(3) thirdListItemOptional.set(listOf("0", "1", "2", "3"), "newValue")
thirdListItemOptional.set(listOf("0", "1", "2"), "newValue")
thirdListItemOptional.setNullable(listOf("0", "1", "2"), "newValue")
Index instances for some common datatypes in both Arrow and the Kotlin stdlib that can be indexed, like
You can look them up by calling
You may create instances of
Index for your own datatypes, which you will be able to use as demonstrated in the example above.
See Deriving and creating custom typeclass to provide your own
Index instances for custom datatypes.
Do you like Arrow?✖