All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.uniflow.result.EitherExt.kt Maven / Gradle / Ivy

Go to download

Simple Unidirectionnel Data Flow for Android & Kotlin, using Kotlin coroutines and open to functional programming

There is a newer version: 1.1.2
Show newest version
package io.uniflow.result

import arrow.core.Either
import arrow.core.orNull
import io.uniflow.core.flow.data.UIEvent
import io.uniflow.core.flow.data.UIState

fun  Either.getOrNull(): A? = orNull()

fun  Either.get(): A =
    fold(
        ifLeft = { throw it },
        ifRight = { it }
    )

fun  Either.get(result: (A) -> R): R =
    fold(
        ifLeft = { throw it },
        ifRight = { result(it) }
    )

suspend fun  Either.onSuccess(f: suspend (A) -> Unit): Either =
    when (this) {
        is Either.Right -> {
            f(b)
            this
        }
        else -> this
    }

suspend fun  Either.onFailure(f: suspend (Throwable) -> Unit): Either =
    when (this) {
        is Either.Left -> {
            f(a)
            this
        }
        else -> this
    }

fun  Either.onValue(f: (A) -> Unit): Either =
    when (this) {
        is Either.Right -> {
            f(b)
            this
        }
        else -> this
    }

suspend fun  Either.toState(onSuccess: suspend (T) -> R): R = onSuccess(get())

suspend fun  Either.toStateOrNull(onSuccess: suspend (T) -> R?): R? =
    fold(
        ifLeft = { null },
        ifRight = { onSuccess(it) }
    )

suspend fun  Either.toState(onSuccess: suspend (T) -> R, onError: suspend (Throwable) -> R): R =
    fold(
        ifLeft = { onError(it) },
        ifRight = { onSuccess(it) }
    )

suspend fun  Either.toEvent(onSuccess: suspend (T) -> R): R = onSuccess(get())

suspend fun  Either.toEvent(onSuccess: suspend (T) -> R, onError: suspend (Throwable) -> R): R =
    fold(
        ifLeft = { onError(it) },
        ifRight = { onSuccess(it) }
    )

suspend fun  Either.toEventOrNull(onSuccess: suspend (T) -> R): R? =
    fold(
        ifLeft = { null },
        ifRight = { onSuccess(it) }
    )





© 2015 - 2025 Weber Informatics LLC | Privacy Policy