Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typealiases for public APIs datatypes #442

Merged
merged 13 commits into from
Nov 6, 2017
4 changes: 2 additions & 2 deletions ank-core/src/main/kotlin/io/kategory/ank/interpreter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ data class Snippet(
val startOffset: Int,
val endOffset: Int,
val code: String,
val result: Option<String> = Option.None)
val result: Option<String> = None)

fun extractCodeImpl(source: String, tree: ASTNode): ListKW<Snippet> {
val sb = mutableListOf<Snippet>()
Expand Down Expand Up @@ -137,7 +137,7 @@ fun compileCodeImpl(origin: File, snippets: ListKW<Snippet>, classpath: ListKW<S
}.fold({
throw CompilationException(cachedEngines.get(snippet.lang), snippet, it)
}, { it })
val resultString = Option.fromNullable(result).fold({ Option.None }, { Option.Some("//$it") })
val resultString = Option.fromNullable(result).fold({ None }, { Some("//$it") })
if (snippet.silent) snippet
else snippet.copy(result = resultString)
}.k()
Expand Down
4 changes: 2 additions & 2 deletions kategory-core/src/main/kotlin/kategory/arrow/Function0.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ data class Function0<out A>(internal val f: () -> A) : Function0Kind<A> {
tailrec fun <A, B> loop(a: A, f: (A) -> HK<Function0HK, Either<A, B>>): B {
val fa = f(a).ev()()
return when (fa) {
is Either.Right<A, B> -> fa.b
is Either.Left<A, B> -> loop(fa.a, f)
is Right<A, B> -> fa.b
is Left<A, B> -> loop(fa.a, f)
}
}

Expand Down
4 changes: 2 additions & 2 deletions kategory-core/src/main/kotlin/kategory/arrow/Function1.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ operator fun <I, O> Function1Kind<I, O>.invoke(i: I): O = this.ev().f(i)
tailrec private fun <I, A, B> step(a: A, t: I, fn: (A) -> Function1Kind<I, Either<A, B>>): B {
val af = fn(a)(t)
return when (af) {
is Either.Right<A, B> -> af.b
is Either.Left<A, B> -> step(af.a, t, fn)
is Right<A, B> -> af.b
is Left<A, B> -> step(af.a, t, fn)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ fun <A, B> case(ff: Tuple2<(A) -> Boolean, (A) -> B>): PartialFunction<A, B> =
infix fun <A, B> ((A) -> Boolean).then(f: (A) -> B): Tuple2<(A) -> Boolean, (A) -> B> = Tuple2(this, f)

private class Lifted<A, B>(val pf: PartialFunction<A, B>) : (A) -> Option<B> {
override fun invoke(x: A): Option<B> = pf.andThen { Option.Some(it) }.applyOrElse(x, { Option.None })
override fun invoke(x: A): Option<B> = pf.andThen { Some(it) }.applyOrElse(x, { None })
}
8 changes: 4 additions & 4 deletions kategory-core/src/main/kotlin/kategory/data/Coproduct.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package kategory

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

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

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

companion object {
Expand Down
33 changes: 18 additions & 15 deletions kategory-core/src/main/kotlin/kategory/data/Either.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package kategory

typealias Right<A, B> = Either.Right<A, B>
typealias Left<A, B> = Either.Left<A, B>

/**
* Port of https://github.com/scala/scala/blob/v2.12.1/src/library/scala/util/Either.scala
*
Expand Down Expand Up @@ -44,16 +47,16 @@ package kategory
fun <C> foldL(b: C, f: (C, B) -> C): C =
this.ev().let { either ->
when (either) {
is Either.Right -> f(b, either.b)
is Either.Left -> b
is Right -> f(b, either.b)
is Left -> b
}
}

fun <C> foldR(lb: Eval<C>, f: (B, Eval<C>) -> Eval<C>): Eval<C> =
this.ev().let { either ->
when (either) {
is Either.Right -> f(either.b, lb)
is Either.Left -> lb
is Right -> f(either.b, lb)
is Left -> lb
}
}

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

/**
* The left side of the disjoint union, as opposed to the [Right] side.
Expand Down Expand Up @@ -140,12 +143,12 @@ package kategory
tailrec fun <L, A, B> tailRecM(a: A, f: (A) -> HK<EitherKindPartial<L>, Either<A, B>>): Either<L, B> {
val ev: Either<L, Either<A, B>> = f(a).ev()
return when (ev) {
is Either.Left<L, Either<A, B>> -> ev.a.left()
is Either.Right<L, Either<A, B>> -> {
is Left<L, Either<A, B>> -> ev.a.left()
is Right<L, Either<A, B>> -> {
val b: Either<A, B> = ev.b
when (b) {
is Either.Left<A, B> -> tailRecM(b.a, f)
is Either.Right<A, B> -> b.b.right()
is Left<A, B> -> tailRecM(b.a, f)
is Right<A, B> -> b.b.right()
}
}
}
Expand All @@ -159,7 +162,7 @@ package kategory
*
* @param f The function to bind across [Either.Right].
*/
inline fun <A, B, C> Either<A, B>.flatMap(crossinline f: (B) -> Either<A, C>): Either<A, C> = fold({ Either.Left(it) }, { f(it) })
inline fun <A, B, C> Either<A, B>.flatMap(crossinline f: (B) -> Either<A, C>): Either<A, C> = fold({ Left(it) }, { f(it) })

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

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

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

fun <A, B> Either<A, B>.combineK(y: EitherKind<A, B>): Either<A, B> =
when (this) {
is Either.Left -> y.ev()
is Left -> y.ev()
else -> this.ev()
}

fun <A> A.left(): Either<A, Nothing> = Either.Left(this)
fun <A> A.left(): Either<A, Nothing> = Left(this)

fun <A> A.right(): Either<Nothing, A> = Either.Right(this)
fun <A> A.right(): Either<Nothing, A> = Right(this)

20 changes: 10 additions & 10 deletions kategory-core/src/main/kotlin/kategory/data/EitherT.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ package kategory
EitherT(MF.tailRecM(a, {
MF.map(f(it).ev().value) { recursionControl ->
when (recursionControl) {
is Either.Left<L, Either<A, B>> -> Either.Right(Either.Left(recursionControl.a))
is Either.Right<L, Either<A, B>> -> {
is Left<L, Either<A, B>> -> Right(Left(recursionControl.a))
is Right<L, Either<A, B>> -> {
val b: Either<A, B> = recursionControl.b
when (b) {
is Either.Left<A, B> -> Either.Left(b.a)
is Either.Right<A, B> -> Either.Right(Either.Right(b.b))
is Left<A, B> -> Left(b.a)
is Right<A, B> -> Right(Right(b.b))
}
}
}
}
}))

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

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

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

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

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

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

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

Expand All @@ -94,8 +94,8 @@ package kategory
fun combineK(y: EitherTKind<F, A, B>, MF: Monad<F>): EitherT<F, A, B> =
EitherT(MF.flatMap(this.ev().value) {
when (it) {
is Either.Left -> y.ev().value
is Either.Right -> MF.pure(it)
is Left -> y.ev().value
is Right -> MF.pure(it)
}
})

Expand Down
4 changes: 2 additions & 2 deletions kategory-core/src/main/kotlin/kategory/data/Eval.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ sealed class Eval<out A> : EvalKind<A> {
fun <A, B> tailRecM(a: A, f: (A) -> EvalKind<Either<A, B>>): Eval<B> =
f(a).ev().flatMap { eval: Either<A, B> ->
when (eval) {
is Either.Left -> tailRecM(eval.a, f)
is Either.Right -> pure(eval.b)
is Left -> tailRecM(eval.a, f)
is Right -> pure(eval.b)
}
}

Expand Down
4 changes: 2 additions & 2 deletions kategory-core/src/main/kotlin/kategory/data/Id.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ data class Id<out A>(val value: A) : IdKind<A> {
tailrec fun <A, B> tailRecM(a: A, f: (A) -> IdKind<Either<A, B>>): Id<B> {
val x: Either<A, B> = f(a).ev().value
return when (x) {
is Either.Left<A, B> -> tailRecM(x.a, f)
is Either.Right<A, B> -> Id(x.b)
is Left<A, B> -> tailRecM(x.a, f)
is Right<A, B> -> Id(x.b)
}
}

Expand Down
22 changes: 11 additions & 11 deletions kategory-core/src/main/kotlin/kategory/data/Ior.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ typealias IorNel<A, B> = Ior<Nel<A>, B>
*/

@JvmStatic fun <A, B> fromOptions(oa: Option<A>, ob: Option<B>): Option<Ior<A, B>> = when (oa) {
is Option.Some -> when (ob) {
is Option.Some -> Option.Some(Both(oa.value, ob.value))
is Option.None -> Option.Some(Left(oa.value))
is Some -> when (ob) {
is Some -> Some(Both(oa.value, ob.value))
is None -> Some(Left(oa.value))
}
is Option.None -> when (ob) {
is Option.Some -> Option.Some(Right(ob.value))
is Option.None -> Option.None
is None -> when (ob) {
is Some -> Some(Right(ob.value))
is None -> None
}
}

Expand Down Expand Up @@ -228,9 +228,9 @@ typealias IorNel<A, B> = Ior<Nel<A>, B>
* ```
*/
fun pad(): Pair<Option<A>, Option<B>> = fold(
{ Pair(Option.Some(it), Option.None) },
{ Pair(Option.None, Option.Some(it)) },
{ a, b -> Pair(Option.Some(a), Option.Some(b)) }
{ Pair(Some(it), None) },
{ Pair(None, Some(it)) },
{ a, b -> Pair(Some(a), Some(b)) }
)

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

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

data class Left<out A>(val value: A) : Ior<A, Nothing>() {
override val isRight: Boolean = false
Expand Down
4 changes: 2 additions & 2 deletions kategory-core/src/main/kotlin/kategory/data/ListKW.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ data class ListKW<out A> constructor(val list: List<A>) : ListKWKind<A>, List<A>
if (!v.isEmpty()) {
val head: Either<A, B> = v.first()
when (head) {
is Either.Right<A, B> -> {
is Right<A, B> -> {
buf += head.b
go(buf, f, v.drop(1).k())
}
is Either.Left<A, B> -> go(buf, f, (f(head.a).ev() + v.drop(1)).k())
is Left<A, B> -> go(buf, f, (f(head.a).ev() + v.drop(1)).k())
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions kategory-core/src/main/kotlin/kategory/data/MapKW.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ fun <K, A> Map<K, A>.k(): MapKW<K, A> = MapKW(this)

fun <K, A> Option<Tuple2<K, A>>.k(): MapKW<K, A> =
when (this) {
is Option.Some -> mapOf(this.value).k()
is Option.None -> emptyMap<K, A>().k()
is Some -> mapOf(this.value).k()
is None -> emptyMap<K, A>().k()
}

fun <K, A> List<Map.Entry<K, A>>.k(): MapKW<K, A> = this.map { it.key to it.value }.toMap().k()
Expand Down
10 changes: 5 additions & 5 deletions kategory-core/src/main/kotlin/kategory/data/NonEmptyList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class NonEmptyList<out A> private constructor(

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

fun <A> pure(a: A): NonEmptyList<A> = a.nel()
Expand All @@ -102,15 +102,15 @@ class NonEmptyList<out A> private constructor(
v: NonEmptyList<Either<A, B>>) {
val head: Either<A, B> = v.head
when (head) {
is Either.Right<A, B> -> {
is Right<A, B> -> {
buf += head.b
val x = NonEmptyList.fromList(v.tail)
when (x) {
is Option.Some<NonEmptyList<Either<A, B>>> -> go(buf, f, x.value)
is Option.None -> Unit
is Some<NonEmptyList<Either<A, B>>> -> go(buf, f, x.value)
is None -> Unit
}
}
is Either.Left<A, B> -> go(buf, f, f(head.a).ev() + v.tail)
is Left<A, B> -> go(buf, f, f(head.a).ev() + v.tail)
}
}

Expand Down
Loading