Skip to content

Commit a9d62a1

Browse files
authored
Merge pull request #442 from kategory/toni-add-typealiases
Add typealiases for public APIs datatypes
2 parents 24b9600 + 699a696 commit a9d62a1

File tree

77 files changed

+352
-326
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+352
-326
lines changed

ank-core/src/main/kotlin/io/kategory/ank/interpreter.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ data class Snippet(
9898
val startOffset: Int,
9999
val endOffset: Int,
100100
val code: String,
101-
val result: Option<String> = Option.None)
101+
val result: Option<String> = None)
102102

103103
fun extractCodeImpl(source: String, tree: ASTNode): ListKW<Snippet> {
104104
val sb = mutableListOf<Snippet>()
@@ -137,7 +137,7 @@ fun compileCodeImpl(origin: File, snippets: ListKW<Snippet>, classpath: ListKW<S
137137
}.fold({
138138
throw CompilationException(cachedEngines.get(snippet.lang), snippet, it)
139139
}, { it })
140-
val resultString = Option.fromNullable(result).fold({ Option.None }, { Option.Some("//$it") })
140+
val resultString = Option.fromNullable(result).fold({ None }, { Some("//$it") })
141141
if (snippet.silent) snippet
142142
else snippet.copy(result = resultString)
143143
}.k()

kategory-core/src/main/kotlin/kategory/arrow/Function0.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ data class Function0<out A>(internal val f: () -> A) : Function0Kind<A> {
3030
tailrec fun <A, B> loop(a: A, f: (A) -> HK<Function0HK, Either<A, B>>): B {
3131
val fa = f(a).ev()()
3232
return when (fa) {
33-
is Either.Right<A, B> -> fa.b
34-
is Either.Left<A, B> -> loop(fa.a, f)
33+
is Right<A, B> -> fa.b
34+
is Left<A, B> -> loop(fa.a, f)
3535
}
3636
}
3737

kategory-core/src/main/kotlin/kategory/arrow/Function1.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ operator fun <I, O> Function1Kind<I, O>.invoke(i: I): O = this.ev().f(i)
2323
tailrec private fun <I, A, B> step(a: A, t: I, fn: (A) -> Function1Kind<I, Either<A, B>>): B {
2424
val af = fn(a)(t)
2525
return when (af) {
26-
is Either.Right<A, B> -> af.b
27-
is Either.Left<A, B> -> step(af.a, t, fn)
26+
is Right<A, B> -> af.b
27+
is Left<A, B> -> step(af.a, t, fn)
2828
}
2929
}
3030

kategory-core/src/main/kotlin/kategory/arrow/PartialFunction.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@ fun <A, B> case(ff: Tuple2<(A) -> Boolean, (A) -> B>): PartialFunction<A, B> =
4545
infix fun <A, B> ((A) -> Boolean).then(f: (A) -> B): Tuple2<(A) -> Boolean, (A) -> B> = Tuple2(this, f)
4646

4747
private class Lifted<A, B>(val pf: PartialFunction<A, B>) : (A) -> Option<B> {
48-
override fun invoke(x: A): Option<B> = pf.andThen { Option.Some(it) }.applyOrElse(x, { Option.None })
48+
override fun invoke(x: A): Option<B> = pf.andThen { Some(it) }.applyOrElse(x, { None })
4949
}

kategory-core/src/main/kotlin/kategory/data/Coproduct.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ package kategory
66

77
fun <B> coflatMap(CF: Comonad<F>, CG: Comonad<G>, f: (Coproduct<F, G, A>) -> B): Coproduct<F, G, B> =
88
Coproduct(run.bimap(
9-
{ CF.coflatMap(it, { f(Coproduct(Either.Left(it))) }) },
10-
{ CG.coflatMap(it, { f(Coproduct(Either.Right(it))) }) }
9+
{ CF.coflatMap(it, { f(Coproduct(Left(it))) }) },
10+
{ CG.coflatMap(it, { f(Coproduct(Right(it))) }) }
1111
))
1212

1313
fun extract(CF: Comonad<F>, CG: Comonad<G>): A = run.fold({ CF.extract(it) }, { CG.extract(it) })
@@ -21,9 +21,9 @@ package kategory
2121

2222
fun <H, B> traverse(f: (A) -> HK<H, B>, GA: Applicative<H>, FT: Traverse<F>, GT: Traverse<G>): HK<H, Coproduct<F, G, B>> =
2323
run.fold({
24-
GA.map(FT.traverse(it, f, GA), { Coproduct<F, G, B>(Either.Left(it)) })
24+
GA.map(FT.traverse(it, f, GA), { Coproduct<F, G, B>(Left(it)) })
2525
}, {
26-
GA.map(GT.traverse(it, f, GA), { Coproduct<F, G, B>(Either.Right(it)) })
26+
GA.map(GT.traverse(it, f, GA), { Coproduct<F, G, B>(Right(it)) })
2727
})
2828

2929
companion object {

kategory-core/src/main/kotlin/kategory/data/Either.kt

+18-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package kategory
22

3+
typealias Right<A, B> = Either.Right<A, B>
4+
typealias Left<A, B> = Either.Left<A, B>
5+
36
/**
47
* Port of https://github.com/scala/scala/blob/v2.12.1/src/library/scala/util/Either.scala
58
*
@@ -44,16 +47,16 @@ package kategory
4447
fun <C> foldL(b: C, f: (C, B) -> C): C =
4548
this.ev().let { either ->
4649
when (either) {
47-
is Either.Right -> f(b, either.b)
48-
is Either.Left -> b
50+
is Right -> f(b, either.b)
51+
is Left -> b
4952
}
5053
}
5154

5255
fun <C> foldR(lb: Eval<C>, f: (B, Eval<C>) -> Eval<C>): Eval<C> =
5356
this.ev().let { either ->
5457
when (either) {
55-
is Either.Right -> f(either.b, lb)
56-
is Either.Left -> lb
58+
is Right -> f(either.b, lb)
59+
is Left -> lb
5760
}
5861
}
5962

@@ -109,7 +112,7 @@ package kategory
109112
* Left(12).toOption() // Result: None
110113
* ```
111114
*/
112-
fun toOption(): Option<B> = fold({ Option.None }, { Option.Some(it) })
115+
fun toOption(): Option<B> = fold({ None }, { Some(it) })
113116

114117
/**
115118
* The left side of the disjoint union, as opposed to the [Right] side.
@@ -140,12 +143,12 @@ package kategory
140143
tailrec fun <L, A, B> tailRecM(a: A, f: (A) -> HK<EitherKindPartial<L>, Either<A, B>>): Either<L, B> {
141144
val ev: Either<L, Either<A, B>> = f(a).ev()
142145
return when (ev) {
143-
is Either.Left<L, Either<A, B>> -> ev.a.left()
144-
is Either.Right<L, Either<A, B>> -> {
146+
is Left<L, Either<A, B>> -> ev.a.left()
147+
is Right<L, Either<A, B>> -> {
145148
val b: Either<A, B> = ev.b
146149
when (b) {
147-
is Either.Left<A, B> -> tailRecM(b.a, f)
148-
is Either.Right<A, B> -> b.b.right()
150+
is Left<A, B> -> tailRecM(b.a, f)
151+
is Right<A, B> -> b.b.right()
149152
}
150153
}
151154
}
@@ -159,7 +162,7 @@ package kategory
159162
*
160163
* @param f The function to bind across [Either.Right].
161164
*/
162-
inline fun <A, B, C> Either<A, B>.flatMap(crossinline f: (B) -> Either<A, C>): Either<A, C> = fold({ Either.Left(it) }, { f(it) })
165+
inline fun <A, B, C> Either<A, B>.flatMap(crossinline f: (B) -> Either<A, C>): Either<A, C> = fold({ Left(it) }, { f(it) })
163166

164167
/**
165168
* Returns the value from this [Either.Right] or the given argument if this is a [Either.Left].
@@ -189,7 +192,7 @@ inline fun <B> Either<*, B>.getOrElse(crossinline default: () -> B): B = fold({
189192
* ```
190193
*/
191194
inline fun <A, B> Either<A, B>.filterOrElse(crossinline predicate: (B) -> Boolean, crossinline default: () -> A): Either<A, B> =
192-
fold({ Either.Left(it) }, { if (predicate(it)) Either.Right(it) else Either.Left(default()) })
195+
fold({ Left(it) }, { if (predicate(it)) Right(it) else Left(default()) })
193196

194197
/**
195198
* Returns `true` if this is a [Either.Right] and its value is equal to `elem` (as determined by `==`),
@@ -210,15 +213,15 @@ fun <A, B> Either<A, B>.contains(elem: B): Boolean = fold({ false }, { it == ele
210213
fun <A, B, C> Either<A, B>.ap(ff: EitherKind<A, (B) -> C>): Either<A, C> = ff.flatMap { f -> map(f) }.ev()
211214

212215
fun <G, A, B, C> Either<A, B>.traverse(f: (B) -> HK<G, C>, GA: Applicative<G>): HK<G, Either<A, C>> =
213-
this.ev().fold({ GA.pure(it.left()) }, { GA.map(f(it), { Either.Right(it) }) })
216+
this.ev().fold({ GA.pure(it.left()) }, { GA.map(f(it), { Right(it) }) })
214217

215218
fun <A, B> Either<A, B>.combineK(y: EitherKind<A, B>): Either<A, B> =
216219
when (this) {
217-
is Either.Left -> y.ev()
220+
is Left -> y.ev()
218221
else -> this.ev()
219222
}
220223

221-
fun <A> A.left(): Either<A, Nothing> = Either.Left(this)
224+
fun <A> A.left(): Either<A, Nothing> = Left(this)
222225

223-
fun <A> A.right(): Either<Nothing, A> = Either.Right(this)
226+
fun <A> A.right(): Either<Nothing, A> = Right(this)
224227

kategory-core/src/main/kotlin/kategory/data/EitherT.kt

+10-10
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ package kategory
1818
EitherT(MF.tailRecM(a, {
1919
MF.map(f(it).ev().value) { recursionControl ->
2020
when (recursionControl) {
21-
is Either.Left<L, Either<A, B>> -> Either.Right(Either.Left(recursionControl.a))
22-
is Either.Right<L, Either<A, B>> -> {
21+
is Left<L, Either<A, B>> -> Right(Left(recursionControl.a))
22+
is Right<L, Either<A, B>> -> {
2323
val b: Either<A, B> = recursionControl.b
2424
when (b) {
25-
is Either.Left<A, B> -> Either.Left(b.a)
26-
is Either.Right<A, B> -> Either.Right(Either.Right(b.b))
25+
is Left<A, B> -> Left(b.a)
26+
is Right<A, B> -> Right(Right(b.b))
2727
}
2828
}
2929
}
3030
}
3131
}))
3232

33-
@JvmStatic fun <F, A, B> right(b: B, MF: Applicative<F>): EitherT<F, A, B> = EitherT(MF.pure(Either.Right(b)))
33+
@JvmStatic fun <F, A, B> right(b: B, MF: Applicative<F>): EitherT<F, A, B> = EitherT(MF.pure(Right(b)))
3434

35-
@JvmStatic fun <F, A, B> left(a: A, MF: Applicative<F>): EitherT<F, A, B> = EitherT(MF.pure(Either.Left(a)))
35+
@JvmStatic fun <F, A, B> left(a: A, MF: Applicative<F>): EitherT<F, A, B> = EitherT(MF.pure(Left(a)))
3636

3737
@JvmStatic inline fun <reified F, A, B> fromEither(value: Either<A, B>, MF: Applicative<F> = monad<F>()): EitherT<F, A, B> =
3838
EitherT(MF.pure(value))
@@ -64,11 +64,11 @@ package kategory
6464
inline fun <C> flatMap(crossinline f: (B) -> EitherT<F, A, C>, MF: Monad<F>): EitherT<F, A, C> = flatMapF({ it -> f(it).value }, MF)
6565

6666
inline fun <C> flatMapF(crossinline f: (B) -> HK<F, Either<A, C>>, MF: Monad<F>): EitherT<F, A, C> =
67-
EitherT(MF.flatMap(value, { either -> either.fold({ MF.pure(Either.Left(it)) }, { f(it) }) }))
67+
EitherT(MF.flatMap(value, { either -> either.fold({ MF.pure(Left(it)) }, { f(it) }) }))
6868

6969
inline fun <C> cata(crossinline l: (A) -> C, crossinline r: (B) -> C, FF: Functor<F>): HK<F, C> = fold(l, r, FF)
7070

71-
fun <C> liftF(fa: HK<F, C>, FF: Functor<F>): EitherT<F, A, C> = EitherT(FF.map(fa, { Either.Right(it) }))
71+
fun <C> liftF(fa: HK<F, C>, FF: Functor<F>): EitherT<F, A, C> = EitherT(FF.map(fa, { Right(it) }))
7272

7373
inline fun <C> semiflatMap(crossinline f: (B) -> HK<F, C>, MF: Monad<F>): EitherT<F, A, C> = flatMap({ liftF(f(it), MF) }, MF)
7474

@@ -94,8 +94,8 @@ package kategory
9494
fun combineK(y: EitherTKind<F, A, B>, MF: Monad<F>): EitherT<F, A, B> =
9595
EitherT(MF.flatMap(this.ev().value) {
9696
when (it) {
97-
is Either.Left -> y.ev().value
98-
is Either.Right -> MF.pure(it)
97+
is Left -> y.ev().value
98+
is Right -> MF.pure(it)
9999
}
100100
})
101101

kategory-core/src/main/kotlin/kategory/data/Eval.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ sealed class Eval<out A> : EvalKind<A> {
4141
fun <A, B> tailRecM(a: A, f: (A) -> EvalKind<Either<A, B>>): Eval<B> =
4242
f(a).ev().flatMap { eval: Either<A, B> ->
4343
when (eval) {
44-
is Either.Left -> tailRecM(eval.a, f)
45-
is Either.Right -> pure(eval.b)
44+
is Left -> tailRecM(eval.a, f)
45+
is Right -> pure(eval.b)
4646
}
4747
}
4848

kategory-core/src/main/kotlin/kategory/data/Id.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ data class Id<out A>(val value: A) : IdKind<A> {
3434
tailrec fun <A, B> tailRecM(a: A, f: (A) -> IdKind<Either<A, B>>): Id<B> {
3535
val x: Either<A, B> = f(a).ev().value
3636
return when (x) {
37-
is Either.Left<A, B> -> tailRecM(x.a, f)
38-
is Either.Right<A, B> -> Id(x.b)
37+
is Left<A, B> -> tailRecM(x.a, f)
38+
is Right<A, B> -> Id(x.b)
3939
}
4040
}
4141

kategory-core/src/main/kotlin/kategory/data/Ior.kt

+11-11
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ typealias IorNel<A, B> = Ior<Nel<A>, B>
7474
*/
7575

7676
@JvmStatic fun <A, B> fromOptions(oa: Option<A>, ob: Option<B>): Option<Ior<A, B>> = when (oa) {
77-
is Option.Some -> when (ob) {
78-
is Option.Some -> Option.Some(Both(oa.value, ob.value))
79-
is Option.None -> Option.Some(Left(oa.value))
77+
is Some -> when (ob) {
78+
is Some -> Some(Both(oa.value, ob.value))
79+
is None -> Some(Left(oa.value))
8080
}
81-
is Option.None -> when (ob) {
82-
is Option.Some -> Option.Some(Right(ob.value))
83-
is Option.None -> Option.None
81+
is None -> when (ob) {
82+
is Some -> Some(Right(ob.value))
83+
is None -> None
8484
}
8585
}
8686

@@ -228,9 +228,9 @@ typealias IorNel<A, B> = Ior<Nel<A>, B>
228228
* ```
229229
*/
230230
fun pad(): Pair<Option<A>, Option<B>> = fold(
231-
{ Pair(Option.Some(it), Option.None) },
232-
{ Pair(Option.None, Option.Some(it)) },
233-
{ a, b -> Pair(Option.Some(a), Option.Some(b)) }
231+
{ Pair(Some(it), None) },
232+
{ Pair(None, Some(it)) },
233+
{ a, b -> Pair(Some(a), Some(b)) }
234234
)
235235

236236
/**
@@ -257,7 +257,7 @@ typealias IorNel<A, B> = Ior<Nel<A>, B>
257257
* Both(12, "power").toOption() // Result: Some("power")
258258
* ```
259259
*/
260-
fun toOption(): Option<B> = fold({ Option.None }, { Option.Some(it) }, { _, b -> Option.Some(b) })
260+
fun toOption(): Option<B> = fold({ None }, { Some(it) }, { _, b -> Some(b) })
261261

262262
/**
263263
* Returns a [Validated.Valid] containing the [Right] value or `B` if this is [Right] or [Both]
@@ -270,7 +270,7 @@ typealias IorNel<A, B> = Ior<Nel<A>, B>
270270
* Both(12, "power").toValidated() // Result: Valid("power")
271271
* ```
272272
*/
273-
fun toValidated(): Validated<A, B> = fold({ Validated.Invalid(it) }, { Validated.Valid(it) }, { _, b -> Validated.Valid(b) })
273+
fun toValidated(): Validated<A, B> = fold({ Invalid(it) }, { Valid(it) }, { _, b -> Valid(b) })
274274

275275
data class Left<out A>(val value: A) : Ior<A, Nothing>() {
276276
override val isRight: Boolean = false

kategory-core/src/main/kotlin/kategory/data/ListKW.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ data class ListKW<out A> constructor(val list: List<A>) : ListKWKind<A>, List<A>
5959
if (!v.isEmpty()) {
6060
val head: Either<A, B> = v.first()
6161
when (head) {
62-
is Either.Right<A, B> -> {
62+
is Right<A, B> -> {
6363
buf += head.b
6464
go(buf, f, v.drop(1).k())
6565
}
66-
is Either.Left<A, B> -> go(buf, f, (f(head.a).ev() + v.drop(1)).k())
66+
is Left<A, B> -> go(buf, f, (f(head.a).ev() + v.drop(1)).k())
6767
}
6868
}
6969
}

kategory-core/src/main/kotlin/kategory/data/MapKW.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ fun <K, A> Map<K, A>.k(): MapKW<K, A> = MapKW(this)
5151

5252
fun <K, A> Option<Tuple2<K, A>>.k(): MapKW<K, A> =
5353
when (this) {
54-
is Option.Some -> mapOf(this.value).k()
55-
is Option.None -> emptyMap<K, A>().k()
54+
is Some -> mapOf(this.value).k()
55+
is None -> emptyMap<K, A>().k()
5656
}
5757

5858
fun <K, A> List<Map.Entry<K, A>>.k(): MapKW<K, A> = this.map { it.key to it.value }.toMap().k()

kategory-core/src/main/kotlin/kategory/data/NonEmptyList.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class NonEmptyList<out A> private constructor(
9090

9191
companion object {
9292
@JvmStatic fun <A> of(head: A, vararg t: A): NonEmptyList<A> = NonEmptyList(head, t.asList())
93-
@JvmStatic fun <A> fromList(l: List<A>): Option<NonEmptyList<A>> = if (l.isEmpty()) Option.None else Option.Some(NonEmptyList(l))
93+
@JvmStatic fun <A> fromList(l: List<A>): Option<NonEmptyList<A>> = if (l.isEmpty()) None else Some(NonEmptyList(l))
9494
@JvmStatic fun <A> fromListUnsafe(l: List<A>): NonEmptyList<A> = NonEmptyList(l)
9595

9696
fun <A> pure(a: A): NonEmptyList<A> = a.nel()
@@ -102,15 +102,15 @@ class NonEmptyList<out A> private constructor(
102102
v: NonEmptyList<Either<A, B>>) {
103103
val head: Either<A, B> = v.head
104104
when (head) {
105-
is Either.Right<A, B> -> {
105+
is Right<A, B> -> {
106106
buf += head.b
107107
val x = NonEmptyList.fromList(v.tail)
108108
when (x) {
109-
is Option.Some<NonEmptyList<Either<A, B>>> -> go(buf, f, x.value)
110-
is Option.None -> Unit
109+
is Some<NonEmptyList<Either<A, B>>> -> go(buf, f, x.value)
110+
is None -> Unit
111111
}
112112
}
113-
is Either.Left<A, B> -> go(buf, f, f(head.a).ev() + v.tail)
113+
is Left<A, B> -> go(buf, f, f(head.a).ev() + v.tail)
114114
}
115115
}
116116

0 commit comments

Comments
 (0)