From 575b3827baa7d74ac723a6131433993ea2935da4 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sat, 17 Jun 2017 11:28:09 -0700 Subject: Add either --- .../ca/allanwang/kau/kpref/items/KPrefCheckbox.kt | 2 -- .../ca/allanwang/kau/kpref/items/KPrefHeader.kt | 1 - .../ca/allanwang/kau/logging/TimberLogger.kt | 1 + .../main/kotlin/ca/allanwang/kau/utils/Either.kt | 32 ++++++++++++++++++++++ .../kotlin/ca/allanwang/kau/utils/Kotterknife.kt | 6 ++-- 5 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 library/src/main/kotlin/ca/allanwang/kau/utils/Either.kt (limited to 'library/src/main/kotlin/ca/allanwang') diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt index 5495c9f..4af837b 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefCheckbox.kt @@ -1,10 +1,8 @@ package ca.allanwang.kau.kpref.items -import android.support.annotation.StringRes import android.view.View import android.widget.CheckBox import ca.allanwang.kau.R -import ca.allanwang.kau.kpref.KPrefAdapterBuilder import ca.allanwang.kau.utils.tint /** diff --git a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt index 4f4192c..294aec0 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/kpref/items/KPrefHeader.kt @@ -2,7 +2,6 @@ package ca.allanwang.kau.kpref.items import android.view.View import ca.allanwang.kau.R -import ca.allanwang.kau.kpref.KPrefAdapterBuilder /** * Created by Allan Wang on 2017-06-07. diff --git a/library/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt b/library/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt index 3b4d651..02f5219 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/logging/TimberLogger.kt @@ -11,6 +11,7 @@ import timber.log.Timber open class TimberLogger(tag: String) { internal val TAG = "$tag: %s" fun e(s: String) = Timber.e(TAG, s) + fun e(t: Throwable) = Timber.e(t, TAG, "error") fun d(s: String) = Timber.d(TAG, s) fun i(s: String) = Timber.i(TAG, s) fun v(s: String) = Timber.v(TAG, s) diff --git a/library/src/main/kotlin/ca/allanwang/kau/utils/Either.kt b/library/src/main/kotlin/ca/allanwang/kau/utils/Either.kt new file mode 100644 index 0000000..dab5810 --- /dev/null +++ b/library/src/main/kotlin/ca/allanwang/kau/utils/Either.kt @@ -0,0 +1,32 @@ +package ca.allanwang.kau.utils + +/** + * Created by Allan Wang on 2017-06-17. + * + * Courtesy of adelnizamutdinov + * + * https://github.com/adelnizamutdinov/kotlin-either + */ +@Suppress("unused") +sealed class Either + +data class Left(val value: T) : Either() +data class Right(val value: T) : Either() + +inline fun Either.fold(left: (L) -> T, right: (R) -> T): T = + when (this) { + is Left -> left(value) + is Right -> right(value) + } + +inline fun Either.flatMap(f: (R) -> Either): Either = + fold({ this as Left }, f) + +inline fun Either.map(f: (R) -> T): Either = + flatMap { Right(f(it)) } + +val Either.isLeft: Boolean + get() = this is Left + +val Either<*, T>.isRight: Boolean + get() = this is Right \ No newline at end of file diff --git a/library/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt b/library/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt index a8ddd2a..fca5677 100644 --- a/library/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt +++ b/library/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt @@ -108,11 +108,11 @@ private fun viewNotFound(id:Int, desc: KProperty<*>): Nothing = @Suppress("UNCHECKED_CAST") private fun required(id: Int, finder: T.(Int) -> View?) - = Lazy { t: T, desc -> t.finder(id) as V? ?: viewNotFound(id, desc) } + = Lazy { t: T, desc -> (t.finder(id) as V?)?.apply { } ?: viewNotFound(id, desc) } @Suppress("UNCHECKED_CAST") private fun optional(id: Int, finder: T.(Int) -> View?) - = Lazy { t: T, desc -> t.finder(id) as V? } + = Lazy { t: T, _ -> t.finder(id) as V? } @Suppress("UNCHECKED_CAST") private fun required(ids: IntArray, finder: T.(Int) -> View?) @@ -120,7 +120,7 @@ private fun required(ids: IntArray, finder: T.(Int) -> View?) @Suppress("UNCHECKED_CAST") private fun optional(ids: IntArray, finder: T.(Int) -> View?) - = Lazy { t: T, desc -> ids.map { t.finder(it) as V? }.filterNotNull() } + = Lazy { t: T, _ -> ids.map { t.finder(it) as V? }.filterNotNull() } // Like Kotlin's lazy delegate but the initializer gets the target and metadata passed to it private class Lazy(private val initializer: (T, KProperty<*>) -> V) : ReadOnlyProperty { -- cgit v1.2.3