-
Notifications
You must be signed in to change notification settings - Fork 439
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
modules/optics/arrow-optics/src/main/kotlin/arrow/optics/instances/EachInstancesJavaUtil.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,44 @@ | ||
package java_util | ||
|
||
import arrow.Kind | ||
import arrow.data.ListK | ||
import arrow.data.MapK | ||
import arrow.data.extract | ||
import arrow.data.k | ||
import arrow.data.traverse | ||
import arrow.optics.Traversal | ||
import arrow.optics.typeclasses.Each | ||
import arrow.typeclasses.Applicative | ||
|
||
interface ListEachInstance<A> : Each<List<A>, A> { | ||
override fun each() = 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.k(), f, FA).let { | ||
FA.map(it) { | ||
it.list | ||
} | ||
} | ||
} | ||
} | ||
|
||
object ListEachInstanceImplicits { | ||
@JvmStatic | ||
fun <A> instance(): Each<List<A>, A> = object : ListEachInstance<A> {} | ||
} | ||
|
||
interface MapKEachInstance<K, V> : Each<Map<K, V>, V> { | ||
override fun each() = 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>> = | ||
MapK.traverse<K>().traverse(s.k(), f, FA).let { | ||
FA.map(it) { | ||
it.extract().map | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
object MapEachInstanceImplicits { | ||
@JvmStatic | ||
fun <K, V> instance(): Each<Map<K, V>, V> = object : MapKEachInstance<K, V> {} | ||
} |
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