-
Notifications
You must be signed in to change notification settings - Fork 449
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Arrow Optics, DSL syntax improvements (#3427)
- Loading branch information
Showing
9 changed files
with
372 additions
and
0 deletions.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
38 changes: 38 additions & 0 deletions
38
arrow-libs/optics/arrow-optics/src/commonMain/kotlin/arrow/optics/dsl/filterIndex.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,38 @@ | ||
package arrow.optics.dsl | ||
|
||
import arrow.core.NonEmptyList | ||
import arrow.core.Predicate | ||
import arrow.optics.Optional | ||
import arrow.optics.Traversal | ||
import arrow.optics.typeclasses.FilterIndex | ||
import kotlin.jvm.JvmName | ||
|
||
/** | ||
* DSL to compose [FilterIndex] with an [Traversal] for a structure [S] to focus in on [A] at given index [I] | ||
* | ||
* @receiver [Optional] with a focus in [S] | ||
* @param filter [FilterIndex] instance to provide a [Optional] to focus into [S] at [I] | ||
* @param i index [I] to focus into [S] and find focus [A] | ||
* @return [Optional] with a focus in [A] at given index [I] | ||
*/ | ||
public fun <T, S, I, A> Traversal<T, S>.filter(filter: FilterIndex<S, I, A>, predicate: Predicate<I>): Traversal<T, A> = | ||
this.compose(filter.filter(predicate)) | ||
|
||
public fun <T, A> Traversal<T, List<A>>.filter(predicate: Predicate<Int>): Traversal<T, A> = | ||
this.compose(FilterIndex.list<A>().filter(predicate)) | ||
|
||
@JvmName("filterNonEmptyList") | ||
public fun <T, A> Traversal<T, NonEmptyList<A>>.filter(predicate: Predicate<Int>): Traversal<T, A> = | ||
this.compose(FilterIndex.nonEmptyList<A>().filter(predicate)) | ||
|
||
@JvmName("filterSequence") | ||
public fun <T, A> Traversal<T, Sequence<A>>.filter(predicate: Predicate<Int>): Traversal<T, A> = | ||
this.compose(FilterIndex.sequence<A>().filter(predicate)) | ||
|
||
@JvmName("filterValues") | ||
public fun <T, K, A> Traversal<T, Map<K, A>>.filter(predicate: Predicate<K>): Traversal<T, A> = | ||
this.compose(FilterIndex.map<K, A>().filter(predicate)) | ||
|
||
@JvmName("filterChars") | ||
public fun <T> Traversal<T, String>.filter(predicate: Predicate<Int>): Traversal<T, Char> = | ||
this.compose(FilterIndex.string().filter(predicate)) |
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
60 changes: 60 additions & 0 deletions
60
arrow-libs/optics/arrow-optics/src/commonTest/kotlin/arrow/optics/dsl/AtSyntaxTest.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,60 @@ | ||
package arrow.optics.dsl | ||
|
||
import arrow.core.None | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class AtSyntaxTest { | ||
|
||
@Test | ||
fun mapModify() { | ||
val original = mapOf("one" to 1, "two" to 2) | ||
val expected = Wrapper(original.mapValues { (k, i) -> | ||
if (k == "one") i + 1 else i | ||
}) | ||
val actual = Wrapper.lens<Map<String, Int>>() | ||
.at("one") | ||
.some | ||
.modify(Wrapper(original), Int::inc) | ||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun mapRemove() { | ||
val original = mapOf("one" to 1, "two" to 2) | ||
val expected = Wrapper(original - "one") | ||
val actual = Wrapper.lens<Map<String, Int>>() | ||
.at("one") | ||
.set(Wrapper(original), None) | ||
assertEquals(expected, actual) | ||
} | ||
|
||
fun setKeep() { | ||
val original = setOf(1) | ||
val expected = Wrapper(setOf(1, 2)) | ||
val actual = Wrapper.lens<Set<Int>>() | ||
.at(2) | ||
.set(Wrapper(original), false) | ||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun setRemove() { | ||
val original = setOf(1) | ||
val expected = Wrapper(setOf(1, 2)) | ||
val actual = Wrapper.lens<Set<Int>>() | ||
.at(2) | ||
.set(Wrapper(original), true) | ||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun setFilter() { | ||
val original = setOf(1) | ||
val expected = Wrapper(emptySet<Int>()) | ||
val actual = Wrapper.lens<Set<Int>>() | ||
.at(1) | ||
.set(Wrapper(original), false) | ||
assertEquals(expected, actual) | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
arrow-libs/optics/arrow-optics/src/commonTest/kotlin/arrow/optics/dsl/EverySyntaxTest.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,103 @@ | ||
package arrow.optics.dsl | ||
|
||
import arrow.core.* | ||
import arrow.optics.Lens | ||
import kotlin.jvm.JvmInline | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class EverySyntaxTest { | ||
|
||
@Test | ||
fun list() { | ||
val original = listOf(1, 2) | ||
val expected = Wrapper(original.map(Int::inc)) | ||
val actual = Wrapper.lens<List<Int>>() | ||
.every | ||
.modify(Wrapper(original), Int::inc) | ||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun either_right() { | ||
val original: Either<String, Int> = 1.right() | ||
val expected = Wrapper(original.map(Int::inc)) | ||
val actual = Wrapper.lens<Either<String, Int>>() | ||
.every | ||
.modify(Wrapper(original), Int::inc) | ||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun either_left() { | ||
val original: Either<String, Int> = "one".left() | ||
val expected = Wrapper(original) | ||
val actual = Wrapper.lens<Either<String, Int>>() | ||
.every | ||
.modify(Wrapper(original), Int::inc) | ||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun map() { | ||
val original = mapOf("one" to 1, "two" to 2) | ||
val expected = Wrapper(original.mapValues { (_, i) -> i + 1 }) | ||
val actual = Wrapper.lens<Map<String, Int>>() | ||
.every | ||
.modify(Wrapper(original), Int::inc) | ||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun nonEmptyList() { | ||
val original = nonEmptyListOf(1, 2) | ||
val expected = Wrapper(original.map(Int::inc)) | ||
val actual = Wrapper.lens<NonEmptyList<Int>>() | ||
.every | ||
.modify(Wrapper(original), Int::inc) | ||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun some() { | ||
val original = 1.some() | ||
val expected = Wrapper(original.map(Int::inc)) | ||
val actual = Wrapper.lens<Option<Int>>() | ||
.every | ||
.modify(Wrapper(original), Int::inc) | ||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun none() { | ||
val original = none<Int>() | ||
val expected = Wrapper(original.map(Int::inc)) | ||
val actual = Wrapper.lens<Option<Int>>() | ||
.every | ||
.modify(Wrapper(original), Int::inc) | ||
assertEquals(expected, actual) | ||
} | ||
|
||
@Test | ||
fun sequence() { | ||
val original = sequenceOf(1, 2) | ||
val expected = Wrapper(original.map(Int::inc)) | ||
val actual = Wrapper.lens<Sequence<Int>>() | ||
.every | ||
.modify(Wrapper(original), Int::inc) | ||
assertEquals(expected.value.toList(), actual.value.toList()) | ||
} | ||
|
||
private fun String.mapEach(block: (Char) -> Char): String = | ||
map(block).joinToString(separator = "") | ||
|
||
@Test | ||
fun string() { | ||
val original = "abc" | ||
val expected = Wrapper(original.mapEach(Char::titlecaseChar)) | ||
val actual = Wrapper.lens<String>() | ||
.every | ||
.modify(Wrapper(original), Char::titlecaseChar) | ||
assertEquals(expected, actual) | ||
} | ||
} |
Oops, something went wrong.