package com.github.michaelbull.result

/**
 * Invokes a [callback] if this [Result] is [Ok].
 */
inline infix fun <V, E> Result<V, E>.onSuccess(callback: (V) -> Unit) = mapBoth(callback, {})

/**
 * Invokes a [callback] if this [Result] is [Err].
 */
inline infix fun <V, E> Result<V, E>.onFailure(callback: (E) -> Unit) = mapBoth({}, callback)

package com.github.michaelbull.result

/**
 * Invokes an [action] if this [Result] is [Ok].
 */
inline infix fun <V, E> Result<V, E>.onSuccess(action: (V) -> Unit): Result<V, E> {
    if (this is Ok) {
        action(value)
    }

    return this
}

/**
 * Invokes an [action] if this [Result] is [Err].
 */
inline infix fun <V, E> Result<V, E>.onFailure(action: (E) -> Unit): Result<V, E> {
    if (this is Err) {
        action(error)
    }

    return this
}

Return the Result in on{Success,Failure}
Return the Result in on{Success,Failure}

This facilitates chaining onSuccess/onFailure calls that may perform
arbitrary side-effects, such as logging.

michaelbull/kotlin-result