FilterIndex

FilterIndex provides a Traversal that can focus into a structure S and get, set, or modify 0 to N foci whose index I satisfies a predicate.

If the foci A for a structure S can be indexed by I, then a Traversal can be created by FilterIndex that is filtered by a predicate on I.

FilterIndex can easily be created, given a Traverse instance and an indexing function.

import arrow.core.*
import arrow.optics.*
import arrow.optics.typeclasses.*
import arrow.core.extensions.listk.traverse.*

val filterIndexStringByIndex = FilterIndex.fromTraverse<ForListK, String>({ list ->
    list.fix().map {
        it toT it.length
    }
}, ListK.traverse())

Given a FilterIndex instance, we can create a Traversal that filters out the foci that do not match the predicate.

val filter: Traversal<ListKOf<String>, String> = filterIndexStringByIndex.filter { length -> length > 3 }

filter.getAll(listOf("H", "He", "Hel", "Hell", "Hello").k())
// [Hell, Hello]

Arrow provides FilterIndex instances for some common datatypes in both Arrow and the Kotlin stdlib that can be filtered by index, like ListK, and MapK. You can look them up by calling FilterIndex.filterIndex().

import arrow.optics.extensions.listk.filterIndex.*
ListK.filterIndex<Int>().filter { index -> index % 2 == 0 }
            .getAll(listOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9).k())
// [0, 2, 4, 6, 8]

Do you like Arrow?

Arrow Org
<