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

Deprecate Show #321

Merged
merged 7 commits into from Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion arrow-core-data/src/main/kotlin/arrow/core/Const.kt
Expand Up @@ -4,6 +4,7 @@ import arrow.Kind
import arrow.typeclasses.Applicative
import arrow.typeclasses.Semigroup
import arrow.typeclasses.Show
import arrow.typeclasses.ShowDeprecation

class ForConst private constructor() { companion object }
typealias ConstOf<A, T> = arrow.Kind2<ForConst, A, T>
Expand Down Expand Up @@ -37,11 +38,12 @@ data class Const<A, out T>(private val value: A) : ConstOf<A, T> {
fun value(): A =
value

@Deprecated(ShowDeprecation)
fun show(SA: Show<A>): String =
"$Const(${SA.run { value.show() }})"

override fun toString(): String =
show(Show.any())
"$Const($value)"
}

fun <A, T> ConstOf<A, T>.combine(SG: Semigroup<A>, that: ConstOf<A, T>): Const<A, T> =
Expand Down
17 changes: 9 additions & 8 deletions arrow-core-data/src/main/kotlin/arrow/core/Either.kt
Expand Up @@ -1063,7 +1063,7 @@ sealed class Either<out A, out B> : EitherOf<A, B> {
override val isLeft = true
override val isRight = false

override fun toString(): String = show(Show.any(), Show.any())
override fun toString(): String = "Either.Left($a)"

companion object {
operator fun <A> invoke(a: A): Either<A, Nothing> = Left(a)
Expand All @@ -1078,20 +1078,21 @@ sealed class Either<out A, out B> : EitherOf<A, B> {
override val isLeft = false
override val isRight = true

override fun toString(): String = show(Show.any(), Show.any())
override fun toString(): String = "Either.Right($b)"

companion object {
operator fun <B> invoke(b: B): Either<Nothing, B> = Right(b)
}
}

fun show(SL: Show<A>, SR: Show<B>): String = fold(
nomisRev marked this conversation as resolved.
Show resolved Hide resolved
{
"Left(${SL.run { it.show() }})"
},
{
"Right(${SR.run { it.show() }})"
}
{ "Left(${SL.run { it.show() }})" },
{ "Right(${SR.run { it.show() }})" }
)

override fun toString(): String = fold(
{ "Either.Left($it)" },
{ "Either.Right($it)" }
)

fun toValidatedNel(): ValidatedNel<A, B> =
Expand Down
21 changes: 21 additions & 0 deletions arrow-core-data/src/main/kotlin/arrow/core/Eval.kt
Expand Up @@ -474,6 +474,9 @@ sealed class Eval<out A> : EvalOf<A> {
data class Now<out A>(val value: A) : Eval<A>() {
override fun value(): A = value
override fun memoize(): Eval<A> = this

override fun toString(): String =
"Eval.Now($value)"
}

/**
Expand All @@ -492,6 +495,9 @@ sealed class Eval<out A> : EvalOf<A> {

override fun value(): A = value
override fun memoize(): Eval<A> = this

override fun toString(): String =
"Eval.Later(f)"
}

/**
Expand All @@ -505,6 +511,9 @@ sealed class Eval<out A> : EvalOf<A> {
data class Always<out A>(private val f: () -> A) : Eval<A>() {
override fun value(): A = f()
override fun memoize(): Eval<A> = Later(f)

override fun toString(): String =
"Eval.Always(f)"
}

/**
Expand All @@ -515,6 +524,9 @@ sealed class Eval<out A> : EvalOf<A> {
data class Defer<out A>(val thunk: () -> Eval<A>) : Eval<A>() {
override fun memoize(): Eval<A> = Memoize(this)
override fun value(): A = collapse(this).value()

override fun toString(): String =
"Eval.Defer(thunk)"
}

/**
Expand All @@ -532,6 +544,9 @@ sealed class Eval<out A> : EvalOf<A> {
abstract fun <S> run(s: S): Eval<A>
override fun memoize(): Eval<A> = Memoize(this)
override fun value(): A = evaluate(this)

override fun toString(): String =
"Eval.FlatMao(..)"
nomisRev marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -547,7 +562,13 @@ sealed class Eval<out A> : EvalOf<A> {
override fun value(): A = result.getOrElse {
evaluate(eval).also { result = Some(it) }
}

override fun toString(): String =
"Eval.Memoize($eval)"
}

override fun toString(): String =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering why we need the toString method in the sealed class

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's not needed if it's defined for all the cases.

"Eval(...)"
}

fun <A, B> Iterator<A>.iterateRight(lb: Eval<B>, f: (A, Eval<B>) -> Eval<B>): Eval<B> {
Expand Down
38 changes: 23 additions & 15 deletions arrow-core-data/src/main/kotlin/arrow/core/Ior.kt
Expand Up @@ -7,7 +7,9 @@ import arrow.typeclasses.Semigroup
import arrow.typeclasses.Show

@Deprecated("Kind is deprecated, and will be removed in 0.13.0. Please use one of the provided concrete methods instead")
class ForIor private constructor() { companion object }
class ForIor private constructor() {
companion object
}
@Deprecated("Kind is deprecated, and will be removed in 0.13.0. Please use one of the provided concrete methods instead")
typealias IorOf<A, B> = arrow.Kind2<ForIor, A, B>
@Deprecated("Kind is deprecated, and will be removed in 0.13.0. Please use one of the provided concrete methods instead")
Expand Down Expand Up @@ -596,7 +598,7 @@ sealed class Ior<out A, out B> : IorOf<A, B> {
override val isLeft: Boolean get() = true
override val isBoth: Boolean get() = false

override fun toString(): String = "Left(${ value.toString() })"
override fun toString(): String = "Ior.Left($value)"

companion object {
@Deprecated("Deprecated, use the constructor instead", ReplaceWith("Left(a)"))
Expand All @@ -609,7 +611,7 @@ sealed class Ior<out A, out B> : IorOf<A, B> {
override val isLeft: Boolean get() = false
override val isBoth: Boolean get() = false

override fun toString(): String = "Right(${ value.toString() })"
override fun toString(): String = "Ior.Right($value)"

companion object {
@Deprecated("Deprecated, use the constructor instead", ReplaceWith("Right(a)"))
Expand All @@ -622,7 +624,7 @@ sealed class Ior<out A, out B> : IorOf<A, B> {
override val isLeft: Boolean get() = false
override val isBoth: Boolean get() = true

override fun toString(): String = "Both(${ leftValue.toString() }, ${ rightValue.toString() })"
override fun toString(): String = "Ior.Both($leftValue, rightValue)"
}

@Deprecated(
Expand All @@ -642,6 +644,12 @@ sealed class Ior<out A, out B> : IorOf<A, B> {
{ a, b -> "Both(${SL.run { a.show() }}, ${SR.run { b.show() }})" }
)

override fun toString(): String = fold(
{ "Ior.Left($it" },
{ "Ior.Right($it)" },
{ a, b -> "Ior.Both($a, $b)" }
)

inline fun <C, D> bicrosswalk(
fa: (A) -> Iterable<C>,
fb: (B) -> Iterable<D>
Expand All @@ -650,7 +658,7 @@ sealed class Ior<out A, out B> : IorOf<A, B> {
{ a -> fa(a).map { it.leftIor() } },
{ b -> fb(b).map { it.rightIor() } },
{ a, b -> fa(a).align(fb(b)) }
)
)

inline fun <C, D, K> bicrosswalkMap(
fa: (A) -> Map<K, C>,
Expand All @@ -660,7 +668,7 @@ sealed class Ior<out A, out B> : IorOf<A, B> {
{ a -> fa(a).mapValues { it.value.leftIor() } },
{ b -> fb(b).mapValues { it.value.rightIor() } },
{ a, b -> fa(a).align(fb(b)) }
)
)

inline fun <C, D> bicrosswalkNull(
fa: (A) -> C?,
Expand Down Expand Up @@ -704,21 +712,21 @@ sealed class Ior<out A, out B> : IorOf<A, B> {
fold(
{ emptyList() },
{ b -> fa(b).map { Right(it) } },
{ a, b -> fa(b).map { Both(a, it) }}
{ a, b -> fa(b).map { Both(a, it) } }
)

inline fun <K, V> crosswalkMap(fa: (B) -> Map<K, V>): Map<K, Ior<A, V>> =
fold(
{ emptyMap() },
{ b -> fa(b).mapValues { Right(it.value) } },
{ a, b -> fa(b).mapValues { Both(a, it.value) }}
{ a, b -> fa(b).mapValues { Both(a, it.value) } }
)

inline fun <A, B, C> crosswalkNull(ior: Ior<A, B>, fa: (B) -> C?): Ior<A, C>? =
ior.fold(
{ a -> Left(a) },
{ b -> fa(b)?.let { Right(it) } },
{ a, b -> fa(b)?.let { Both(a, it) }}
{ a, b -> fa(b)?.let { Both(a, it) } }
)

inline fun all(predicate: (B) -> Boolean): Boolean =
Expand Down Expand Up @@ -794,21 +802,21 @@ sealed class Ior<out A, out B> : IorOf<A, B> {
fold(
{ a -> listOf(Left(a)) },
{ b -> fa(b).map { Right(it) } },
{ a, b -> fa(b).map { Both(a, it) }}
{ a, b -> fa(b).map { Both(a, it) } }
)

inline fun <AA, C> traverseEither(fa: (B) -> Either<AA, C>): Either<AA, Ior<A, C>> =
fold(
{ a -> Either.right(Left(a)) },
{ b -> fa(b).map { Right(it) } },
{ a, b -> fa(b).map { Both(a, it) }}
{ a, b -> fa(b).map { Both(a, it) } }
)

inline fun <AA, C> traverseValidated(fa: (B) -> Validated<AA, C>): Validated<AA, Ior<A, C>> =
fold(
{ a -> Valid(Left(a)) },
{ b -> fa(b).map { Right(it) } },
{ a, b -> fa(b).map { Both(a, it) }}
{ a, b -> fa(b).map { Both(a, it) } }
)

inline fun <C> traverse_(fa: (B) -> Iterable<C>): List<Unit> =
Expand Down Expand Up @@ -977,7 +985,7 @@ fun <A, B> Ior<A, B>.replicate(SA: Semigroup<A>, n: Int): Ior<A, List<B>> =
is Ior.Right -> Ior.Right(List(n) { value })
is Ior.Left -> this
is Ior.Both -> bimap(
{ List(n - 1) { leftValue}.fold(leftValue, { acc, a -> SA.run { acc + a }}) },
{ List(n - 1) { leftValue }.fold(leftValue, { acc, a -> SA.run { acc + a } }) },
{ List(n) { rightValue } }
)
}
Expand All @@ -988,7 +996,7 @@ fun <A, B> Ior<A, B>.replicate(SA: Semigroup<A>, n: Int, MB: Monoid<B>): Ior<A,
is Ior.Right -> Ior.Right(MB.run { List(n) { value }.combineAll() })
is Ior.Left -> this
is Ior.Both -> bimap(
{ List(n - 1) { leftValue}.fold(leftValue, { acc, a -> SA.run { acc + a }}) },
{ List(n - 1) { leftValue }.fold(leftValue, { acc, a -> SA.run { acc + a } }) },
{ MB.run { List(n) { rightValue }.combineAll() } }
)
}
Expand Down Expand Up @@ -1039,7 +1047,7 @@ fun <A, B, C> Ior<A, B>.zip(SA: Semigroup<A>, fb: Ior<A, C>): Ior<A, Pair<B, C>>
zip(SA, fb, ::Pair)

fun <A, B, C, Z> Ior<A, B>.zipEval(SA: Semigroup<A>, other: Eval<Ior<A, C>>, f: (B, C) -> Z): Eval<Ior<A, Z>> =
other.map {zip(SA, it).map { a -> f(a.first, a.second) }}
other.map { zip(SA, it).map { a -> f(a.first, a.second) } }

fun <A, B> Semigroup.Companion.ior(SA: Semigroup<A>, SB: Semigroup<B>): Semigroup<Ior<A, B>> =
IorSemigroup(SA, SB)
Expand Down
4 changes: 0 additions & 4 deletions arrow-core-data/src/main/kotlin/arrow/core/Iterable.kt
Expand Up @@ -5,7 +5,6 @@ package arrow.core
import arrow.typeclasses.Eq
import arrow.typeclasses.Monoid
import arrow.typeclasses.Semigroup
import arrow.typeclasses.Show
import kotlin.collections.foldRight as _foldRight

inline fun <A, B> Iterable<A>.foldRight(initial: B, operation: (A, acc: B) -> B): B =
Expand Down Expand Up @@ -267,9 +266,6 @@ inline fun <A, B, C> Iterable<A>.rightPadZip(other: Iterable<B>, fa: (A, B?) ->
fun <A, B> Iterable<A>.rightPadZip(other: Iterable<B>): List<Tuple2<A, B?>> =
this.rightPadZip(other) { a, b -> a toT b }

fun <A> Iterable<A>.show(SA: Show<A>): String = "[" +
joinToString(", ") { SA.run { it.show() } } + "]"

@Suppress("UNCHECKED_CAST")
private tailrec fun <A, B> go(
buf: MutableList<B>,
Expand Down
7 changes: 0 additions & 7 deletions arrow-core-data/src/main/kotlin/arrow/core/List.kt
Expand Up @@ -5,7 +5,6 @@ import arrow.typeclasses.Hash
import arrow.typeclasses.Monoid
import arrow.typeclasses.Order
import arrow.typeclasses.Semigroup
import arrow.typeclasses.Show
import arrow.typeclasses.defaultSalt
import arrow.typeclasses.hashWithSalt
import kotlin.collections.plus as _plus
Expand Down Expand Up @@ -125,9 +124,3 @@ object ListMonoid : Monoid<List<Any?>> {
override fun empty(): List<Any?> = emptyList()
override fun List<Any?>.combine(b: List<Any?>): List<Any?> = this._plus(b)
}

fun <A> Show.Companion.list(SA: Show<A>): Show<List<A>> =
object : Show<List<A>> {
override fun List<A>.show(): String =
show(SA)
}
14 changes: 10 additions & 4 deletions arrow-core-data/src/main/kotlin/arrow/core/ListK.kt
Expand Up @@ -3,8 +3,11 @@ package arrow.core
import arrow.Kind
import arrow.typeclasses.Applicative
import arrow.typeclasses.Show
import arrow.typeclasses.ShowDeprecation

class ForListK private constructor() { companion object }
class ForListK private constructor() {
companion object
}
typealias ListKOf<A> = arrow.Kind<ForListK, A>

@Suppress("UNCHECKED_CAST", "NOTHING_TO_INLINE")
Expand Down Expand Up @@ -420,10 +423,13 @@ data class ListK<out A>(private val list: List<A>) : ListKOf<A>, List<A> by list
): ListK<Tuple2<A, B?>> =
this.rightPadZip(other) { a, b -> a toT b }

fun show(SA: Show<A>): String = "[" +
list.joinToString(", ") { SA.run { it.show() } } + "]"
@Deprecated(ShowDeprecation)
fun show(SA: Show<A>): String = SA.run {
joinToString(prefix = "[", separator = ", ", postfix = "]") { it.show() }
}

override fun toString(): String = show(Show.any())
override fun toString(): String =
list.toString()

companion object {

Expand Down
5 changes: 4 additions & 1 deletion arrow-core-data/src/main/kotlin/arrow/core/MapK.kt
Expand Up @@ -3,6 +3,7 @@ package arrow.core
import arrow.Kind
import arrow.typeclasses.Applicative
import arrow.typeclasses.Show
import arrow.typeclasses.ShowDeprecation

@Deprecated("Kind is deprecated, and will be removed in 0.13.0. Please use one of the provided concrete methods instead")
class ForMapK private constructor() { companion object }
Expand Down Expand Up @@ -54,9 +55,11 @@ data class MapK<K, out A>(private val map: Map<K, A>) : MapKOf<K, A>, Map<K, A>
}.value()
}

@Deprecated(ShowDeprecation)
fun show(SK: Show<K>, SA: Show<A>): String = "Map(${toList().k().map { it.toTuple2() }.show(Show { show(SK, SA) })})"

override fun toString(): String = show(Show.any(), Show.any())
override fun toString(): String =
map.toString()

override fun equals(other: Any?): Boolean =
when (other) {
Expand Down