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

arrow.generic.coproduct3.Coproduct3.kt Maven / Gradle / Ivy

package arrow.generic.coproduct3

import arrow.core.Option
import arrow.core.toOption
import kotlin.Suppress
import kotlin.Unit

/**
 * Represents a sealed hierarchy of 3 types where only one of the types is actually present.
 */
sealed class Coproduct3

/**
 * Represents the first type of a Coproduct3
 */
data class First(val a: A) : Coproduct3()

/**
 * Represents the second type of a Coproduct3
 */
data class Second(val b: B) : Coproduct3()

/**
 * Represents the third type of a Coproduct3
 */
data class Third(val c: C) : Coproduct3()

/**
 * Creates a Coproduct from the A type
 *
 * @return A Coproduct3 where the receiver is the A
 */
fun  A.first(): Coproduct3 = First(this)

/**
 * Creates a Coproduct from the B type
 *
 * @return A Coproduct3 where the receiver is the B
 */
fun  B.second(): Coproduct3 = Second(this)

/**
 * Creates a Coproduct from the C type
 *
 * @return A Coproduct3 where the receiver is the C
 */
fun  C.third(): Coproduct3 = Third(this)

/**
 * Transforms the Coproduct into an Option based on the actual value of the Coproduct
 *
 * @return None if the Coproduct was not the specified type, Some if it was the specified type
 */
fun  Coproduct3.select(): Option = (this as? First)?.a.toOption()

/**
 * Transforms the Coproduct into an Option based on the actual value of the Coproduct
 *
 * @return None if the Coproduct was not the specified type, Some if it was the specified type
 */
@Suppress("UNUSED_PARAMETER")
fun  Coproduct3<*, B, *>.select(dummy0: Unit = Unit): Option = (this as? Second)?.b.toOption()

/**
 * Transforms the Coproduct into an Option based on the actual value of the Coproduct
 *
 * @return None if the Coproduct was not the specified type, Some if it was the specified type
 */
@Suppress("UNUSED_PARAMETER")
fun  Coproduct3<*, *, C>.select(dummy0: Unit = Unit, dummy1: Unit = Unit): Option = (this as?
        Third)?.c.toOption()

/**
 * Runs the function related to the actual value of the Coproduct and returns the result
 *
 * @param a The function used to map A to the RESULT type
 * @param b The function used to map B to the RESULT type
 * @param c The function used to map C to the RESULT type
 *
 * @return RESULT generated by one of the input functions
 */
fun  Coproduct3.fold(
    a: (A) -> RESULT,
    b: (B) -> RESULT,
    c: (C) -> RESULT
): RESULT = when (this) {
    is First -> a(this.a)
    is Second -> b(this.b)
    is Third -> c(this.c)
}