-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FilterIndex instances for java_util
- Loading branch information
Showing
3 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...ptics/arrow-optics/src/main/kotlin/arrow/optics/instances/FilterIndexInstancesJavaUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package java_util | ||
|
||
import arrow.Kind | ||
import arrow.core.Predicate | ||
import arrow.core.toT | ||
import arrow.data.ListK | ||
import arrow.data.k | ||
import arrow.data.traverse | ||
import arrow.optics.Traversal | ||
import arrow.optics.typeclasses.FilterIndex | ||
import arrow.typeclasses.Applicative | ||
|
||
interface MapFilterIndexInstance<K, V> : FilterIndex<Map<K, V>, K, V> { | ||
override fun filter(p: Predicate<K>) = object : Traversal<Map<K, V>, V> { | ||
override fun <F> modifyF(FA: Applicative<F>, s: Map<K, V>, f: (V) -> Kind<F, V>): Kind<F, Map<K, V>> = | ||
ListK.traverse().traverse(s.toList().k(), { (k, v) -> | ||
FA.map(if (p(k)) f(v) else FA.pure(v)) { | ||
k to it | ||
} | ||
}, FA).let { | ||
FA.map(it) { | ||
it.toMap() | ||
} | ||
} | ||
} | ||
} | ||
|
||
object MapFilterIndexInstanceImplicits { | ||
@JvmStatic | ||
fun <K, V> instance(): FilterIndex<Map<K, V>, K, V> = object : MapFilterIndexInstance<K, V> {} | ||
} | ||
|
||
interface ListFilterIndexInstance<A> : FilterIndex<List<A>, Int, A> { | ||
override fun filter(p: (Int) -> Boolean): Traversal<List<A>, A> = object : Traversal<List<A>, A> { | ||
override fun <F> modifyF(FA: Applicative<F>, s: List<A>, f: (A) -> Kind<F, A>): Kind<F, List<A>> = | ||
ListK.traverse().traverse(s.mapIndexed { index, a -> a toT index }.k(), { (a, j) -> | ||
if (p(j)) f(a) else FA.pure(a) | ||
}, FA).let { | ||
FA.map(it) { | ||
it.list | ||
} | ||
} | ||
} | ||
} | ||
|
||
object ListFilterIndexInstanceImplicits { | ||
@JvmStatic | ||
fun <A> instance(): FilterIndex<List<A>, Int, A> = object : ListFilterIndexInstance<A> {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters