From 7309f6be46dc5a8ecb2746a82aec6b4190de8de7 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sun, 5 Feb 2023 20:32:55 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B5=D0=BC=D0=BD=D0=BE=D0=B3=D0=BE=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=B4=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D0=BB=20either?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/dbotthepony/kstarbound/io/json/EitherTypeAdapter.kt | 2 +- src/main/kotlin/ru/dbotthepony/kstarbound/util/Either.kt | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/io/json/EitherTypeAdapter.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/io/json/EitherTypeAdapter.kt index dd34ffae..4a827759 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/io/json/EitherTypeAdapter.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/io/json/EitherTypeAdapter.kt @@ -13,7 +13,7 @@ import java.lang.reflect.ParameterizedType /** * При объявлении [Either] для (де)сериализации *НЕОБХОДИМО* объявить - * такое *левое* свойство, которое имеет [TypeAdapter] который не "засоряет" JsonReader + * такое *левое* свойство, которое имеет [TypeAdapter] который не "засоряет" [JsonReader] */ object EitherTypeAdapter : TypeAdapterFactory { override fun create(gson: Gson, type: TypeToken): TypeAdapter? { diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/util/Either.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/util/Either.kt index 8f735733..ddc7c3d4 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/util/Either.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/util/Either.kt @@ -20,6 +20,13 @@ data class Either(val left: L?, val right: R?) { right.invoke(this.right!!) } + inline fun reduce(left: (L) -> T, right: (R) -> T): T { + return if (this.left != null) + left.invoke(this.left) + else + right.invoke(this.right!!) + } + companion object { @JvmStatic fun left(value: L): Either {