Skip to content

Commit 4c13011

Browse files
authored
Deprecate Each typeclass (#81)
* Deprecate Each typeclass * Remove unused annotation * Replace each with traversal in tests
1 parent 2be011a commit 4c13011

File tree

10 files changed

+215
-41
lines changed

10 files changed

+215
-41
lines changed

arrow-libs/optics/arrow-optics/src/main/kotlin/arrow/optics/Traversal.kt

+62
Original file line numberDiff line numberDiff line change
@@ -367,4 +367,66 @@ interface PTraversal<S, T, A, B> : PTraversalOf<S, T, A, B> {
367367
* Check if forall targets satisfy the predicate
368368
*/
369369
fun forall(s: S, p: (A) -> Boolean): Boolean = foldMap(s, p, AndMonoid)
370+
371+
/**
372+
* DSL to compose [Traversal] with a [Lens] for a structure [S] to see all its foci [A]
373+
*
374+
* @receiver [Lens] with a focus in [S]
375+
* @return [Traversal] with a focus in [A]
376+
*/
377+
val <U, V> PLens<U, V, S, T>.every: PTraversal<U, V, A, B> get() =
378+
this@every.compose(this@PTraversal)
379+
380+
/**
381+
* DSL to compose [Traversal] with a [Iso] for a structure [S] to see all its foci [A]
382+
*
383+
* @receiver [Iso] with a focus in [S]
384+
* @return [Traversal] with a focus in [A]
385+
*/
386+
val <U, V> PIso<U, V, S, T>.every: PTraversal<U, V, A, B> get() =
387+
this@every.compose(this@PTraversal)
388+
389+
/**
390+
* DSL to compose [Traversal] with a [Prism] for a structure [S] to see all its foci [A]
391+
*
392+
* @receiver [Prism] with a focus in [S]
393+
* @return [Traversal] with a focus in [A]
394+
*/
395+
val <U, V> PPrism<U, V, S, T>.every: PTraversal<U, V, A, B> get() =
396+
this.compose(this@PTraversal)
397+
398+
/**
399+
* DSL to compose [Traversal] with a [Optional] for a structure [S] to see all its foci [A]
400+
*
401+
* @receiver [Optional] with a focus in [S]
402+
* @return [Traversal] with a focus in [A]
403+
*/
404+
val <U, V> POptional<U, V, S, T>.every: PTraversal<U, V, A, B> get() =
405+
this.compose(this@PTraversal)
406+
407+
/**
408+
* DSL to compose [Traversal] with a [Setter] for a structure [S] to see all its foci [A]
409+
*
410+
* @receiver [Setter] with a focus in [S]
411+
* @return [Setter] with a focus in [A]
412+
*/
413+
val <U, V> PSetter<U, V, S, T>.every: PSetter<U, V, A, B> get() =
414+
this.compose(this@PTraversal)
415+
416+
/**
417+
* DSL to compose [Traversal] with a [Traversal] for a structure [S] to see all its foci [A]
418+
*
419+
* @receiver [Traversal] with a focus in [S]
420+
* @return [Traversal] with a focus in [A]
421+
*/
422+
val <U, V> PTraversal<U, V, S, T>.every: PTraversal<U, V, A, B> get() =
423+
this.compose(this@PTraversal)
424+
425+
/**
426+
* DSL to compose [Traversal] with a [Fold] for a structure [S] to see all its foci [A]
427+
*
428+
* @receiver [Fold] with a focus in [S]
429+
* @return [Fold] with a focus in [A]
430+
*/
431+
val <U> Fold<U, S>.every: Fold<U, A> get() = this.compose(this@PTraversal.asFold())
370432
}

arrow-libs/optics/arrow-optics/src/main/kotlin/arrow/optics/dsl/each.kt

+35
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ import arrow.optics.typeclasses.Each
1616
* @param EA [Each] to provide [Traversal] that can focus into a structure [S] to see all its foci [A]
1717
* @return [Traversal] with a focus in [A]
1818
*/
19+
@Deprecated(
20+
"Each is being deprecated. Use Traversal directly instead.",
21+
ReplaceWith("every(TR: Traversal<S, A>)", "arrow.optics.Traversal"),
22+
DeprecationLevel.WARNING
23+
)
1924
fun <T, S, A> Lens<T, S>.every(EA: Each<S, A>): Traversal<T, A> = this.compose(EA.each())
2025

2126
/**
@@ -25,6 +30,11 @@ fun <T, S, A> Lens<T, S>.every(EA: Each<S, A>): Traversal<T, A> = this.compose(E
2530
* @param EA [Each] to provide [Traversal] that can focus into a structure [S] to see all its foci [A]
2631
* @return [Traversal] with a focus in [A]
2732
*/
33+
@Deprecated(
34+
"Each is being deprecated. Use Traversal directly instead.",
35+
ReplaceWith("every(TR: Traversal<S, A>)", "arrow.optics.Traversal"),
36+
DeprecationLevel.WARNING
37+
)
2838
fun <T, S, A> Iso<T, S>.every(EA: Each<S, A>): Traversal<T, A> = this.compose(EA.each())
2939

3040
/**
@@ -34,6 +44,11 @@ fun <T, S, A> Iso<T, S>.every(EA: Each<S, A>): Traversal<T, A> = this.compose(EA
3444
* @param EA [Each] to provide [Traversal] that can focus into a structure [S] to see all its foci [A]
3545
* @return [Traversal] with a focus in [A]
3646
*/
47+
@Deprecated(
48+
"Each is being deprecated. Use Traversal directly instead.",
49+
ReplaceWith("every(TR: Traversal<S, A>)", "arrow.optics.Traversal"),
50+
DeprecationLevel.WARNING
51+
)
3752
fun <T, S, A> Prism<T, S>.every(EA: Each<S, A>): Traversal<T, A> = this.compose(EA.each())
3853

3954
/**
@@ -43,6 +58,11 @@ fun <T, S, A> Prism<T, S>.every(EA: Each<S, A>): Traversal<T, A> = this.compose(
4358
* @param EA [Each] to provide [Traversal] that can focus into a structure [S] to see all its foci [A]
4459
* @return [Traversal] with a focus in [A]
4560
*/
61+
@Deprecated(
62+
"Each is being deprecated. Use Traversal directly instead.",
63+
ReplaceWith("every(TR: Traversal<S, A>)", "arrow.optics.Traversal"),
64+
DeprecationLevel.WARNING
65+
)
4666
fun <T, S, A> Optional<T, S>.every(EA: Each<S, A>): Traversal<T, A> = this.compose(EA.each())
4767

4868
/**
@@ -52,6 +72,11 @@ fun <T, S, A> Optional<T, S>.every(EA: Each<S, A>): Traversal<T, A> = this.compo
5272
* @param EA [Each] to provide [Traversal] that can focus into a structure [S] to see all its foci [A]
5373
* @return [Setter] with a focus in [A]
5474
*/
75+
@Deprecated(
76+
"Each is being deprecated. Use Traversal directly instead.",
77+
ReplaceWith("every(TR: Traversal<S, A>)", "arrow.optics.Traversal"),
78+
DeprecationLevel.WARNING
79+
)
5580
fun <T, S, A> Setter<T, S>.every(EA: Each<S, A>): Setter<T, A> = this.compose(EA.each())
5681

5782
/**
@@ -61,6 +86,11 @@ fun <T, S, A> Setter<T, S>.every(EA: Each<S, A>): Setter<T, A> = this.compose(EA
6186
* @param EA [Each] to provide [Traversal] that can focus into a structure [S] to see all its foci [A]
6287
* @return [Traversal] with a focus in [A]
6388
*/
89+
@Deprecated(
90+
"Each is being deprecated. Use Traversal directly instead.",
91+
ReplaceWith("every(TR: Traversal<S, A>)", "arrow.optics.Traversal"),
92+
DeprecationLevel.WARNING
93+
)
6494
fun <T, S, A> Traversal<T, S>.every(EA: Each<S, A>): Traversal<T, A> = this.compose(EA.each())
6595

6696
/**
@@ -70,4 +100,9 @@ fun <T, S, A> Traversal<T, S>.every(EA: Each<S, A>): Traversal<T, A> = this.comp
70100
* @param EA [Each] to provide [Traversal] that can focus into a structure [S] to see all its foci [A]
71101
* @return [Fold] with a focus in [A]
72102
*/
103+
@Deprecated(
104+
"Each is being deprecated. Use Traversal directly instead.",
105+
ReplaceWith("every(TR: Traversal<S, A>)", "arrow.optics.Traversal"),
106+
DeprecationLevel.WARNING
107+
)
73108
fun <T, S, A> Fold<T, S>.every(EA: Each<S, A>): Fold<T, A> = this.compose(EA.each())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package arrow.optics.dsl
2+
3+
import arrow.optics.Fold
4+
import arrow.optics.Iso
5+
import arrow.optics.Lens
6+
import arrow.optics.Optional
7+
import arrow.optics.Prism
8+
import arrow.optics.Setter
9+
import arrow.optics.Traversal
10+
11+
/**
12+
* DSL to compose [Traversal] with a [Lens] for a structure [S] to see all its foci [A]
13+
*
14+
* @receiver [Lens] with a focus in [S]
15+
* @param TR [Traversal] that can focus into a structure [S] to see all its foci [A]
16+
* @return [Traversal] with a focus in [A]
17+
*/
18+
fun <T, S, A> Lens<T, S>.every(TR: Traversal<S, A>): Traversal<T, A> = this.compose(TR)
19+
20+
/**
21+
* DSL to compose [Traversal] with an [Iso] for a structure [S] to see all its foci [A]
22+
*
23+
* @receiver [Iso] with a focus in [S]
24+
* @param TR [Traversal] that can focus into a structure [S] to see all its foci [A]
25+
* @return [Traversal] with a focus in [A]
26+
*/
27+
fun <T, S, A> Iso<T, S>.every(TR: Traversal<S, A>): Traversal<T, A> = this.compose(TR)
28+
29+
/**
30+
* DSL to compose [Traversal] with a [Prism] for a structure [S] to see all its foci [A]
31+
*
32+
* @receiver [Prism] with a focus in [S]
33+
* @param TR [Traversal] that can focus into a structure [S] to see all its foci [A]
34+
* @return [Traversal] with a focus in [A]
35+
*/
36+
fun <T, S, A> Prism<T, S>.every(TR: Traversal<S, A>): Traversal<T, A> = this.compose(TR)
37+
38+
/**
39+
* DSL to compose [Traversal] with an [Optional] for a structure [S] to see all its foci [A]
40+
*
41+
* @receiver [Optional] with a focus in [S]
42+
* @param TR [Traversal] that can focus into a structure [S] to see all its foci [A]
43+
* @return [Traversal] with a focus in [A]
44+
*/
45+
fun <T, S, A> Optional<T, S>.every(TR: Traversal<S, A>): Traversal<T, A> = this.compose(TR)
46+
47+
/**
48+
* DSL to compose [Traversal] with a [Setter] for a structure [S] to see all its foci [A]
49+
*
50+
* @receiver [Setter] with a focus in [S]
51+
* @param TR [Traversal] that can focus into a structure [S] to see all its foci [A]
52+
* @return [Setter] with a focus in [A]
53+
*/
54+
fun <T, S, A> Setter<T, S>.every(TR: Traversal<S, A>): Setter<T, A> = this.compose(TR)
55+
56+
/**
57+
* DSL to compose [Traversal] with a [Traversal] for a structure [S] to see all its foci [A]
58+
*
59+
* @receiver [Traversal] with a focus in [S]
60+
* @param TR [Traversal] that can focus into a structure [S] to see all its foci [A]
61+
* @return [Traversal] with a focus in [A]
62+
*/
63+
fun <T, S, A> Traversal<T, S>.every(TR: Traversal<S, A>): Traversal<T, A> = this.compose(TR)
64+
65+
/**
66+
* DSL to compose [Traversal] with a [Fold] for a structure [S] to see all its foci [A]
67+
*
68+
* @receiver [Fold] with a focus in [S]
69+
* @param TR [Traversal] that can focus into a structure [S] to see all its foci [A]
70+
* @return [Fold] with a focus in [A]
71+
*/
72+
fun <T, S, A> Fold<T, S>.every(TR: Traversal<S, A>): Fold<T, A> = this.compose(TR)

arrow-libs/optics/arrow-optics/src/main/kotlin/arrow/optics/typeclasses/Each.kt

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import arrow.typeclasses.Traverse
1818
* @param S source of the [Traversal]
1919
* @param A focus of [Traversal]
2020
*/
21+
@Deprecated("Each is being deprecated. Use Traversal directly instead.")
2122
fun interface Each<S, A> {
2223

2324
/**
@@ -33,6 +34,7 @@ fun interface Each<S, A> {
3334
* @receiver [Lens] with a focus in [S]
3435
* @return [Traversal] with a focus in [A]
3536
*/
37+
@Deprecated("Each is being deprecated. Use Traversal directly instead.")
3638
val <T> Lens<T, S>.every: Traversal<T, A> get() = this.compose(each())
3739

3840
/**
@@ -41,6 +43,7 @@ fun interface Each<S, A> {
4143
* @receiver [Iso] with a focus in [S]
4244
* @return [Traversal] with a focus in [A]
4345
*/
46+
@Deprecated("Each is being deprecated. Use Traversal directly instead.")
4447
val <T> Iso<T, S>.every: Traversal<T, A> get() = this.compose(each())
4548

4649
/**
@@ -49,6 +52,7 @@ fun interface Each<S, A> {
4952
* @receiver [Prism] with a focus in [S]
5053
* @return [Traversal] with a focus in [A]
5154
*/
55+
@Deprecated("Each is being deprecated. Use Traversal directly instead.")
5256
val <T> Prism<T, S>.every: Traversal<T, A> get() = this.compose(each())
5357

5458
/**
@@ -57,6 +61,7 @@ fun interface Each<S, A> {
5761
* @receiver [Optional] with a focus in [S]
5862
* @return [Traversal] with a focus in [A]
5963
*/
64+
@Deprecated("Each is being deprecated. Use Traversal directly instead.")
6065
val <T> Optional<T, S>.every: Traversal<T, A> get() = this.compose(each())
6166

6267
/**
@@ -65,6 +70,7 @@ fun interface Each<S, A> {
6570
* @receiver [Setter] with a focus in [S]
6671
* @return [Setter] with a focus in [A]
6772
*/
73+
@Deprecated("Each is being deprecated. Use Traversal directly instead.")
6874
val <T> Setter<T, S>.every: Setter<T, A> get() = this.compose(each())
6975

7076
/**
@@ -73,6 +79,7 @@ fun interface Each<S, A> {
7379
* @receiver [Traversal] with a focus in [S]
7480
* @return [Traversal] with a focus in [A]
7581
*/
82+
@Deprecated("Each is being deprecated. Use Traversal directly instead.")
7683
val <T> Traversal<T, S>.every: Traversal<T, A> get() = this.compose(each())
7784

7885
/**
@@ -81,6 +88,7 @@ fun interface Each<S, A> {
8188
* @receiver [Fold] with a focus in [S]
8289
* @return [Fold] with a focus in [A]
8390
*/
91+
@Deprecated("Each is being deprecated. Use Traversal directly instead.")
8492
val <T> Fold<T, S>.every: Fold<T, A> get() = this.compose(each())
8593

8694
companion object {

arrow-libs/optics/arrow-optics/src/test/kotlin/arrow/optics/DSLTest.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import arrow.core.k
77
import arrow.optics.dsl.at
88
import arrow.optics.extensions.listk.index.index
99
import arrow.optics.extensions.mapk.at.at
10-
import arrow.optics.extensions.mapk.each.each
1110
import arrow.optics.extensions.traversal
1211
import arrow.core.test.UnitSpec
1312
import io.kotlintest.shouldBe
@@ -102,8 +101,8 @@ class BoundedTest : UnitSpec() {
102101
} shouldBe (Db.content compose MapK.at<Keys, String>().at(One)).set(db, None)
103102
}
104103

105-
"Working with Each in Optics should be same as in DSL" {
106-
MapK.each<Keys, String>().run {
104+
"Working with Traversal in Optics should be same as in DSL" {
105+
MapK.traversal<Keys, String>().run {
107106
Db.content.every.modify(db, String::toUpperCase)
108107
} shouldBe (Db.content compose MapK.traversal()).modify(db, String::toUpperCase)
109108
}

arrow-libs/optics/arrow-optics/src/test/kotlin/arrow/optics/instances/ListInstanceTest.kt

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
package arrow.optics.instances
22

33
import arrow.core.ListExtensions
4+
import arrow.core.ListK
45
import arrow.core.Option
56
import arrow.core.Tuple2
67
import arrow.core.extensions.eq
7-
import arrow.core.ListK
88
import arrow.core.extensions.listk.eq.eq
99
import arrow.core.extensions.option.eq.eq
1010
import arrow.core.extensions.tuple2.eq.eq
11-
import arrow.optics.extensions.each
11+
import arrow.core.test.UnitSpec
12+
import arrow.core.test.generators.functionAToB
13+
import arrow.core.test.generators.listK
14+
import arrow.core.test.generators.tuple2
1215
import arrow.optics.extensions.filterIndex
1316
import arrow.optics.extensions.index
1417
import arrow.optics.extensions.listk.cons.cons
15-
import arrow.optics.extensions.listk.each.each
1618
import arrow.optics.extensions.listk.filterIndex.filterIndex
1719
import arrow.optics.extensions.listk.index.index
1820
import arrow.optics.extensions.listk.snoc.snoc
19-
import arrow.core.test.UnitSpec
20-
import arrow.core.test.generators.functionAToB
21-
import arrow.core.test.generators.listK
22-
import arrow.core.test.generators.tuple2
21+
import arrow.optics.extensions.traversal
2322
import arrow.optics.test.laws.OptionalLaws
2423
import arrow.optics.test.laws.PrismLaws
2524
import arrow.optics.test.laws.TraversalLaws
@@ -32,7 +31,7 @@ class ListInstanceTest : UnitSpec() {
3231

3332
testLaws(
3433
TraversalLaws.laws(
35-
traversal = ListK.each<String>().each(),
34+
traversal = ListK.traversal(),
3635
aGen = Gen.listK(Gen.string()),
3736
bGen = Gen.string(),
3837
funcGen = Gen.functionAToB(Gen.string()),
@@ -44,7 +43,7 @@ class ListInstanceTest : UnitSpec() {
4443

4544
testLaws(
4645
TraversalLaws.laws(
47-
traversal = ListExtensions.each<String>().each(),
46+
traversal = ListExtensions.traversal(),
4847
aGen = Gen.list(Gen.string()),
4948
bGen = Gen.string(),
5049
funcGen = Gen.functionAToB(Gen.string()),

0 commit comments

Comments
 (0)