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

Deprecate extensions methods for Map and MapK #87

Merged
merged 7 commits into from Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
141 changes: 129 additions & 12 deletions arrow-optics/src/main/kotlin/arrow/optics/extensions/map.kt
Expand Up @@ -3,10 +3,13 @@ package arrow.optics.extensions
import arrow.Kind
import arrow.core.MapInstances
import arrow.core.Option
import arrow.core.left
import arrow.core.right
import arrow.core.Predicate
import arrow.core.getOption
import arrow.core.k
import arrow.core.left
import arrow.core.right
import arrow.optics.Lens
import arrow.optics.Optional
import arrow.optics.PLens
import arrow.optics.POptional
import arrow.optics.Traversal
Expand All @@ -16,13 +19,28 @@ import arrow.optics.typeclasses.FilterIndex
import arrow.optics.typeclasses.Index
import arrow.typeclasses.Applicative

fun <K, V> MapInstances.at(): At<Map<K, V>, K, Option<V>> = mapAt()
@Deprecated(
"Typeclass instance have been moved to the companion object of the typeclass.",
ReplaceWith(
"At.map<K, V>()",
"arrow.optics.map", "arrow.optics.typeclasses.At"
),
DeprecationLevel.WARNING)
fun <K, V> MapInstances.at(): At<Map<K, V>, K, Option<V>> = MapAt()

/**
* [At] instance definition for [Map].
*/
fun <K, V> mapAt(): At<Map<K, V>, K, Option<V>> = At { i ->
PLens(
@Deprecated(
"Typeclass interface implementation will not be exposed directly anymore.",
ReplaceWith(
"At.map<K, V>()",
"arrow.optics.map", "arrow.optics.typeclasses.At"
),
DeprecationLevel.WARNING
)
interface MapAt<K, V> : At<Map<K, V>, K, Option<V>> {
override fun at(i: K): Lens<Map<K, V>, Option<V>> = PLens(
get = { it.getOption(i) },
set = { map, optV ->
optV.fold({
Expand All @@ -32,13 +50,37 @@ fun <K, V> mapAt(): At<Map<K, V>, K, Option<V>> = At { i ->
})
}
)

companion object {
/**
* Operator overload to instantiate typeclass instance.
*
* @return [Index] instance for [String]
*/
operator fun <K, V> invoke() = object : MapAt<K, V> {}
}
}

@Deprecated(
"Typeclass instance have been moved to the companion object of the typeclass.",
ReplaceWith(
"Traversal.map<K, V>()",
"arrow.optics.Traversal", "arrow.optics.map"
),
DeprecationLevel.WARNING)
fun <K, V> MapInstances.traversal(): Traversal<Map<K, V>, V> = MapTraversal()

/**
* [Traversal] for [Map] that focuses in each [V] of the source [Map].
*/
@Deprecated(
"Typeclass interface implementation will not be exposed directly anymore.",
ReplaceWith(
"Traversal.map<K, V>()",
"arrow.optics.Traversal", "arrow.optics.map"
),
DeprecationLevel.WARNING
)
interface MapTraversal<K, V> : 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>> = FA.run {
s.k().traverse(FA, f)
Expand All @@ -54,20 +96,62 @@ interface MapTraversal<K, V> : Traversal<Map<K, V>, V> {
}
}

fun <K, V> MapInstances.each(): Each<Map<K, V>, V> = mapEach()
@Deprecated(
"Each is being deprecated. Use the instance for Map from Traversal's companion object instead.",
ReplaceWith(
"Traversal.map<K, V>()",
"arrow.optics.Traversal", "arrow.optics.map"
),
DeprecationLevel.WARNING
)
fun <K, V> MapInstances.each(): Each<Map<K, V>, V> = MapEach()

/**
* [Each] instance definition for [Map].
*/
fun <K, V> mapEach(): Each<Map<K, V>, V> = Each { MapTraversal() }
@Deprecated(
"Each is being deprecated. Use the instance for Map from Traversal's companion object instead.",
ReplaceWith(
"Traversal.map<K, V>()",
"arrow.optics.Traversal", "arrow.optics.map"
),
DeprecationLevel.WARNING
)
interface MapEach<K, V> : Each<Map<K, V>, V> {
override fun each() = MapTraversal<K, V>()

companion object {
/**
* Operator overload to instantiate typeclass instance.
*
* @return [Index] instance for [String]
*/
operator fun <K, V> invoke() = object : MapEach<K, V> {}
}
}

@Deprecated(
"Typeclass instance have been moved to the companion object of the typeclass.",
ReplaceWith(
"FilterIndex.map<K, V>()",
"arrow.optics.map", "arrow.optics.typeclasses.FilterIndex"
),
DeprecationLevel.WARNING)
fun <K, V> MapInstances.filterIndex(): FilterIndex<Map<K, V>, K, V> = filterMapIndex()

/**
* [FilterIndex] instance definition for [Map].
*/
fun <K, V> filterMapIndex(): FilterIndex<Map<K, V>, K, V> = FilterIndex { p ->
object : Traversal<Map<K, V>, V> {
@Deprecated(
"Typeclass interface implementation will not be exposed directly anymore.",
ReplaceWith(
"FilterIndex.map<K, V>()",
"arrow.optics.map", "arrow.optics.typeclasses.FilterIndex"
),
DeprecationLevel.WARNING
)
interface filterMapIndex<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>> = FA.run {
s.toList().k().traverse(FA) { (k, v) ->
(if (p(k)) f(v) else just(v)).map {
Expand All @@ -76,16 +160,49 @@ fun <K, V> filterMapIndex(): FilterIndex<Map<K, V>, K, V> = FilterIndex { p ->
}.map { it.toMap() }
}
}

companion object {
/**
* Operator overload to instantiate typeclass instance.
*
* @return [Index] instance for [String]
*/
operator fun <K, V> invoke() = object : filterMapIndex<K, V> {}
}
}

fun <K, V> MapInstances.index(): Index<Map<K, V>, K, V> = mapIndex()
@Deprecated(
"Typeclass instance have been moved to the companion object of the typeclass.",
ReplaceWith(
"Index.map<K, V>()",
"arrow.optics.map", "arrow.optics.typeclasses.Index"
),
DeprecationLevel.WARNING)
fun <K, V> MapInstances.index(): Index<Map<K, V>, K, V> = MapIndex()

/**
* [Index] instance definition for [Map].
*/
fun <K, V> mapIndex(): Index<Map<K, V>, K, V> = Index { i ->
POptional(
@Deprecated(
"Typeclass interface implementation will not be exposed directly anymore.",
ReplaceWith(
"Index.map<K, V>()",
"arrow.optics.map", "arrow.optics.typeclasses.Index"
),
DeprecationLevel.WARNING
)
interface MapIndex<K, V> : Index<Map<K, V>, K, V> {
override fun index(i: K): Optional<Map<K, V>, V> = POptional(
getOrModify = { it[i]?.right() ?: it.left() },
set = { m, v -> m.mapValues { (k, vv) -> if (k == i) v else vv } }
)

companion object {
/**
* Operator overload to instantiate typeclass instance.
*
* @return [Index] instance for [String]
*/
operator fun <K, V> invoke() = object : MapIndex<K, V> {}
}
}
@@ -0,0 +1,48 @@
package arrow.optics.extensions.map.at

import arrow.core.MapK
import arrow.core.Option
import arrow.optics.PLens
import arrow.optics.extensions.MapKAt

@JvmName("at")
@Suppress(
"UNCHECKED_CAST",
"USELESS_CAST",
"EXTENSION_SHADOWED_BY_MEMBER",
"UNUSED_PARAMETER"
)
@Deprecated(
"arrow.optics.extensions package is being deprecated, and it will be removed in 0.13.",
ReplaceWith(
"this compose At.map<K, V>().at(i)",
"arrow.optics.map", "arrow.optics.typeclasses.At", "arrow.optics.compose"
),
level = DeprecationLevel.WARNING
)
fun <K, V, T> PLens<T, T, MapK<K, V>, MapK<K, V>>.at(i: K): PLens<T, T, Option<V>, Option<V>> =
arrow.optics.extensions.map.at.Map.at<K, V>().run {
this@at.at<T>(i) as arrow.optics.PLens<T, T, arrow.core.Option<V>, arrow.core.Option<V>>
}

/**
* cached extension
*/
@PublishedApi()
internal val at_singleton: MapKAt<Any?, Any?> = object : MapKAt<Any?, Any?> {}

@Deprecated("Receiver Map object is deprecated, and it will be removed in 0.13.")
object Map {
@Suppress(
"UNCHECKED_CAST",
"NOTHING_TO_INLINE"
)
@Deprecated(
"Typeclass instance have been moved to the companion object of the typeclass.",
ReplaceWith(
"At.map<K, V>()",
"arrow.optics.map", "arrow.optics.typeclasses.At"
),
DeprecationLevel.WARNING
)
inline fun <K, V> at(): MapKAt<K, V> = at_singleton as arrow.optics.extensions.MapKAt<K, V>}
@@ -0,0 +1,45 @@
package arrow.optics.extensions.map.each

import arrow.core.MapK
import arrow.optics.PTraversal
import arrow.optics.extensions.MapKEach

@JvmName("each")
@Suppress(
"UNCHECKED_CAST",
"USELESS_CAST",
"EXTENSION_SHADOWED_BY_MEMBER",
"UNUSED_PARAMETER"
)
@Deprecated(
"Each is being deprecated. Use the instance for Map from Traversal's companion object instead.",
ReplaceWith(
"Traversal.map<K, V>()",
"arrow.optics.Traversal", "arrow.optics.map"),
DeprecationLevel.WARNING
)
fun <K, V> each(): PTraversal<MapK<K, V>, MapK<K, V>, V, V> = arrow.optics.extensions.map.each.Map
.each<K, V>()
.each() as arrow.optics.PTraversal<arrow.core.MapK<K, V>, arrow.core.MapK<K, V>, V, V>

/**
* cached extension
*/
@PublishedApi()
internal val each_singleton: MapKEach<Any?, Any?> = object : MapKEach<Any?, Any?> {}

@Deprecated("Receiver Map object is deprecated, and it will be removed in 0.13.")
object Map {
@Suppress(
"UNCHECKED_CAST",
"NOTHING_TO_INLINE"
)
@Deprecated(
"Each is being deprecated. Use the instance for Map from Traversal's companion object instead.",
ReplaceWith(
"Traversal.map<K, V>()",
"arrow.optics.Traversal", "arrow.optics.map"),
DeprecationLevel.WARNING
)
inline fun <K, V> each(): MapKEach<K, V> = each_singleton as arrow.optics.extensions.MapKEach<K, V>
}
@@ -0,0 +1,41 @@
package arrow.optics.extensions.map.filterIndex

import arrow.core.MapK
import arrow.optics.PTraversal
import arrow.optics.extensions.MapKFilterIndex

@JvmName("filter")
@Suppress(
"UNCHECKED_CAST",
"USELESS_CAST",
"EXTENSION_SHADOWED_BY_MEMBER",
"UNUSED_PARAMETER"
)
@Deprecated(
"arrow.optics.extensions package is being deprecated. Use the exposed function in the instance for Map from the companion object of the typeclass instead.",
ReplaceWith(
"FilterIndex.map<K, V>().filter(p)",
"arrow.optics.map", "arrow.optics.typeclasses.FilterIndex"
),
DeprecationLevel.WARNING
)
fun <K, V> filter(p: Function1<K, Boolean>): PTraversal<MapK<K, V>, MapK<K, V>, V, V> =
arrow.optics.extensions.map.filterIndex.Map
.filterIndex<K, V>()
.filter(p) as arrow.optics.PTraversal<arrow.core.MapK<K, V>, arrow.core.MapK<K, V>, V, V>

/**
* cached extension
*/
@PublishedApi()
internal val filterIndex_singleton: MapKFilterIndex<Any?, Any?> = object : MapKFilterIndex<Any?,
Any?> {}

@Deprecated("Receiver Map object is deprecated, and it will be removed in 0.13.")
object Map {
@Suppress(
"UNCHECKED_CAST",
"NOTHING_TO_INLINE"
)
inline fun <K, V> filterIndex(): MapKFilterIndex<K, V> = filterIndex_singleton as
arrow.optics.extensions.MapKFilterIndex<K, V>}
@@ -0,0 +1,49 @@
package arrow.optics.extensions.map.index

import arrow.core.MapK
import arrow.optics.PLens
import arrow.optics.POptional
import arrow.optics.extensions.MapKIndex

@JvmName("index")
@Suppress(
"UNCHECKED_CAST",
"USELESS_CAST",
"EXTENSION_SHADOWED_BY_MEMBER",
"UNUSED_PARAMETER"
)
@Deprecated(
"arrow.optics.extensions package is being deprecated, and it will be removed in 0.13.",
ReplaceWith(
"this compose Index.map<K, V>().index(i)",
"arrow.optics.map", "arrow.optics.typeclasses.Index", "arrow.optics.compose"
),
level = DeprecationLevel.WARNING
)
fun <K, V, T> PLens<T, T, MapK<K, V>, MapK<K, V>>.index(i: K): POptional<T, T, V, V> =
arrow.optics.extensions.map.index.Map.index<K, V>().run {
this@index.index<T>(i) as arrow.optics.POptional<T, T, V, V>
}

/**
* cached extension
*/
@PublishedApi()
internal val index_singleton: MapKIndex<Any?, Any?> = object : MapKIndex<Any?, Any?> {}

@Deprecated("Receiver Map object is deprecated, and it will be removed in 0.13.")
object Map {
@Suppress(
"UNCHECKED_CAST",
"NOTHING_TO_INLINE"
)
@Deprecated(
"Typeclass instance have been moved to the companion object of the typeclass.",
ReplaceWith(
"Index.map<K, V>()",
"arrow.optics.map", "arrow.optics.typeclasses.Index"
),
DeprecationLevel.WARNING
)
inline fun <K, V> index(): MapKIndex<K, V> = index_singleton as
arrow.optics.extensions.MapKIndex<K, V>}