Skip to content
This repository has been archived by the owner on Feb 24, 2021. It is now read-only.

Deprecate Each typeclass #81

Merged
merged 5 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 62 additions & 0 deletions arrow-optics/src/main/kotlin/arrow/optics/Traversal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -367,4 +367,66 @@ interface PTraversal<S, T, A, B> : PTraversalOf<S, T, A, B> {
* Check if forall targets satisfy the predicate
*/
fun forall(s: S, p: (A) -> Boolean): Boolean = foldMap(s, p, AndMonoid)

/**
* DSL to compose [Traversal] with a [Lens] for a structure [S] to see all its foci [A]
*
* @receiver [Lens] with a focus in [S]
* @return [Traversal] with a focus in [A]
*/
val <U, V> PLens<U, V, S, T>.every: PTraversal<U, V, A, B> get() =
this@every.compose(this@PTraversal)

/**
* DSL to compose [Traversal] with a [Iso] for a structure [S] to see all its foci [A]
*
* @receiver [Iso] with a focus in [S]
* @return [Traversal] with a focus in [A]
*/
val <U, V> PIso<U, V, S, T>.every: PTraversal<U, V, A, B> get() =
this@every.compose(this@PTraversal)

/**
* DSL to compose [Traversal] with a [Prism] for a structure [S] to see all its foci [A]
*
* @receiver [Prism] with a focus in [S]
* @return [Traversal] with a focus in [A]
*/
val <U, V> PPrism<U, V, S, T>.every: PTraversal<U, V, A, B> get() =
this.compose(this@PTraversal)

/**
* DSL to compose [Traversal] with a [Optional] for a structure [S] to see all its foci [A]
*
* @receiver [Optional] with a focus in [S]
* @return [Traversal] with a focus in [A]
*/
val <U, V> POptional<U, V, S, T>.every: PTraversal<U, V, A, B> get() =
this.compose(this@PTraversal)

/**
* DSL to compose [Traversal] with a [Setter] for a structure [S] to see all its foci [A]
*
* @receiver [Setter] with a focus in [S]
* @return [Setter] with a focus in [A]
*/
val <U, V> PSetter<U, V, S, T>.every: PSetter<U, V, A, B> get() =
this.compose(this@PTraversal)

/**
* DSL to compose [Traversal] with a [Traversal] for a structure [S] to see all its foci [A]
*
* @receiver [Traversal] with a focus in [S]
* @return [Traversal] with a focus in [A]
*/
val <U, V> PTraversal<U, V, S, T>.every: PTraversal<U, V, A, B> get() =
this.compose(this@PTraversal)

/**
* DSL to compose [Traversal] with a [Fold] for a structure [S] to see all its foci [A]
*
* @receiver [Fold] with a focus in [S]
* @return [Fold] with a focus in [A]
*/
val <U> Fold<U, S>.every: Fold<U, A> get() = this.compose(this@PTraversal.asFold())
}
35 changes: 35 additions & 0 deletions arrow-optics/src/main/kotlin/arrow/optics/dsl/each.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import arrow.optics.typeclasses.Each
* @param EA [Each] to provide [Traversal] that can focus into a structure [S] to see all its foci [A]
* @return [Traversal] with a focus in [A]
*/
@Deprecated(
"Each is being deprecated. Use Traversal directly instead.",
ReplaceWith("every(TR: Traversal<S, A>)", "arrow.optics.Traversal"),
DeprecationLevel.WARNING
)
fun <T, S, A> Lens<T, S>.every(EA: Each<S, A>): Traversal<T, A> = this.compose(EA.each())

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

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

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

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

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

/**
Expand All @@ -70,4 +100,9 @@ fun <T, S, A> Traversal<T, S>.every(EA: Each<S, A>): Traversal<T, A> = this.comp
* @param EA [Each] to provide [Traversal] that can focus into a structure [S] to see all its foci [A]
* @return [Fold] with a focus in [A]
*/
@Deprecated(
"Each is being deprecated. Use Traversal directly instead.",
ReplaceWith("every(TR: Traversal<S, A>)", "arrow.optics.Traversal"),
DeprecationLevel.WARNING
)
fun <T, S, A> Fold<T, S>.every(EA: Each<S, A>): Fold<T, A> = this.compose(EA.each())
72 changes: 72 additions & 0 deletions arrow-optics/src/main/kotlin/arrow/optics/dsl/every.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package arrow.optics.dsl

import arrow.optics.Fold
import arrow.optics.Iso
import arrow.optics.Lens
import arrow.optics.Optional
import arrow.optics.Prism
import arrow.optics.Setter
import arrow.optics.Traversal

/**
* DSL to compose [Traversal] with a [Lens] for a structure [S] to see all its foci [A]
*
* @receiver [Lens] with a focus in [S]
* @param TR [Traversal] that can focus into a structure [S] to see all its foci [A]
* @return [Traversal] with a focus in [A]
*/
fun <T, S, A> Lens<T, S>.every(TR: Traversal<S, A>): Traversal<T, A> = this.compose(TR)

/**
* DSL to compose [Traversal] with an [Iso] for a structure [S] to see all its foci [A]
*
* @receiver [Iso] with a focus in [S]
* @param TR [Traversal] that can focus into a structure [S] to see all its foci [A]
* @return [Traversal] with a focus in [A]
*/
fun <T, S, A> Iso<T, S>.every(TR: Traversal<S, A>): Traversal<T, A> = this.compose(TR)

/**
* DSL to compose [Traversal] with a [Prism] for a structure [S] to see all its foci [A]
*
* @receiver [Prism] with a focus in [S]
* @param TR [Traversal] that can focus into a structure [S] to see all its foci [A]
* @return [Traversal] with a focus in [A]
*/
fun <T, S, A> Prism<T, S>.every(TR: Traversal<S, A>): Traversal<T, A> = this.compose(TR)

/**
* DSL to compose [Traversal] with an [Optional] for a structure [S] to see all its foci [A]
*
* @receiver [Optional] with a focus in [S]
* @param TR [Traversal] that can focus into a structure [S] to see all its foci [A]
* @return [Traversal] with a focus in [A]
*/
fun <T, S, A> Optional<T, S>.every(TR: Traversal<S, A>): Traversal<T, A> = this.compose(TR)

/**
* DSL to compose [Traversal] with a [Setter] for a structure [S] to see all its foci [A]
*
* @receiver [Setter] with a focus in [S]
* @param TR [Traversal] that can focus into a structure [S] to see all its foci [A]
* @return [Setter] with a focus in [A]
*/
fun <T, S, A> Setter<T, S>.every(TR: Traversal<S, A>): Setter<T, A> = this.compose(TR)

/**
* DSL to compose [Traversal] with a [Traversal] for a structure [S] to see all its foci [A]
*
* @receiver [Traversal] with a focus in [S]
* @param TR [Traversal] that can focus into a structure [S] to see all its foci [A]
* @return [Traversal] with a focus in [A]
*/
fun <T, S, A> Traversal<T, S>.every(TR: Traversal<S, A>): Traversal<T, A> = this.compose(TR)

/**
* DSL to compose [Traversal] with a [Fold] for a structure [S] to see all its foci [A]
*
* @receiver [Fold] with a focus in [S]
* @param TR [Traversal] that can focus into a structure [S] to see all its foci [A]
* @return [Fold] with a focus in [A]
*/
fun <T, S, A> Fold<T, S>.every(TR: Traversal<S, A>): Fold<T, A> = this.compose(TR)
8 changes: 8 additions & 0 deletions arrow-optics/src/main/kotlin/arrow/optics/typeclasses/Each.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import arrow.typeclasses.Traverse
* @param S source of the [Traversal]
* @param A focus of [Traversal]
*/
@Deprecated("Each is being deprecated. Use Traversal directly instead.")
fun interface Each<S, A> {

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

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

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

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

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

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

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

companion object {
Expand Down
5 changes: 2 additions & 3 deletions arrow-optics/src/test/kotlin/arrow/optics/DSLTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import arrow.core.k
import arrow.optics.dsl.at
import arrow.optics.extensions.listk.index.index
import arrow.optics.extensions.mapk.at.at
import arrow.optics.extensions.mapk.each.each
import arrow.optics.extensions.traversal
import arrow.core.test.UnitSpec
import io.kotlintest.shouldBe
Expand Down Expand Up @@ -102,8 +101,8 @@ class BoundedTest : UnitSpec() {
} shouldBe (Db.content compose MapK.at<Keys, String>().at(One)).set(db, None)
}

"Working with Each in Optics should be same as in DSL" {
MapK.each<Keys, String>().run {
"Working with Traversal in Optics should be same as in DSL" {
MapK.traversal<Keys, String>().run {
Db.content.every.modify(db, String::toUpperCase)
} shouldBe (Db.content compose MapK.traversal()).modify(db, String::toUpperCase)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
package arrow.optics.instances

import arrow.core.ListExtensions
import arrow.core.ListK
import arrow.core.Option
import arrow.core.Tuple2
import arrow.core.extensions.eq
import arrow.core.ListK
import arrow.core.extensions.listk.eq.eq
import arrow.core.extensions.option.eq.eq
import arrow.core.extensions.tuple2.eq.eq
import arrow.optics.extensions.each
import arrow.core.test.UnitSpec
import arrow.core.test.generators.functionAToB
import arrow.core.test.generators.listK
import arrow.core.test.generators.tuple2
import arrow.optics.extensions.filterIndex
import arrow.optics.extensions.index
import arrow.optics.extensions.listk.cons.cons
import arrow.optics.extensions.listk.each.each
import arrow.optics.extensions.listk.filterIndex.filterIndex
import arrow.optics.extensions.listk.index.index
import arrow.optics.extensions.listk.snoc.snoc
import arrow.core.test.UnitSpec
import arrow.core.test.generators.functionAToB
import arrow.core.test.generators.listK
import arrow.core.test.generators.tuple2
import arrow.optics.extensions.traversal
import arrow.optics.test.laws.OptionalLaws
import arrow.optics.test.laws.PrismLaws
import arrow.optics.test.laws.TraversalLaws
Expand All @@ -32,7 +31,7 @@ class ListInstanceTest : UnitSpec() {

testLaws(
TraversalLaws.laws(
traversal = ListK.each<String>().each(),
traversal = ListK.traversal(),
aGen = Gen.listK(Gen.string()),
bGen = Gen.string(),
funcGen = Gen.functionAToB(Gen.string()),
Expand All @@ -44,7 +43,7 @@ class ListInstanceTest : UnitSpec() {

testLaws(
TraversalLaws.laws(
traversal = ListExtensions.each<String>().each(),
traversal = ListExtensions.traversal(),
aGen = Gen.list(Gen.string()),
bGen = Gen.string(),
funcGen = Gen.functionAToB(Gen.string()),
Expand Down