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

arrow.generic.coproduct5.Coproduct5.kt Maven / Gradle / Ivy

package arrow.generic.coproduct5

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

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

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

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

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

/**
 * Represents the fourth type of a Coproduct5
 */
data class Fourth(val d: D) : Coproduct5()

/**
 * Represents the fifth type of a Coproduct5
 */
data class Fifth(val e: E) : Coproduct5()

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

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

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

/**
 * Creates a Coproduct from the D type
 *
 * @return A Coproduct5 where the receiver is the D
 */
fun  D.fourth(): Coproduct5 = Fourth(this)

/**
 * Creates a Coproduct from the E type
 *
 * @return A Coproduct5 where the receiver is the E
 */
fun  E.fifth(): Coproduct5 = Fifth(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  Coproduct5.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  Coproduct5<*, 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  Coproduct5<*, *, C, *, *>.select(dummy0: Unit = Unit, dummy1: Unit = Unit): Option =
        (this as? Third)?.c.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  Coproduct5<*, *, *, D, *>.select(
    dummy0: Unit = Unit,
    dummy1: Unit = Unit,
    dummy2: Unit = Unit
): Option = (this as? Fourth)?.d.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  Coproduct5<*, *, *, *, E>.select(
    dummy0: Unit = Unit,
    dummy1: Unit = Unit,
    dummy2: Unit = Unit,
    dummy3: Unit = Unit
): Option = (this as? Fifth)?.e.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
 * @param d The function used to map D to the RESULT type
 * @param e The function used to map E to the RESULT type
 *
 * @return RESULT generated by one of the input functions
 */
fun  Coproduct5.fold(
    a: (A) -> RESULT,
    b: (B) -> RESULT,
    c: (C) -> RESULT,
    d: (D) -> RESULT,
    e: (E) -> RESULT
): RESULT = when (this) {
    is First -> a(this.a)
    is Second -> b(this.b)
    is Third -> c(this.c)
    is Fourth -> d(this.d)
    is Fifth -> e(this.e)
}