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

arrow.generic.coproduct6.Coproduct6.kt Maven / Gradle / Ivy

package arrow.generic.coproduct6

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

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

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

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

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

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

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

/**
 * Represents the sixth type of a Coproduct6
 */
data class Sixth(val f: F) : Coproduct6()

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

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

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

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

/**
 * Creates a Coproduct from the E type
 *
 * @return A Coproduct6 where the receiver is the E
 */
fun  E.fifth(): Coproduct6 = Fifth(this)

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