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

arrow.data.SetKW.kt Maven / Gradle / Ivy

package arrow.data

import arrow.core.Eval
import arrow.higherkind

@higherkind
data class SetKW(val set: Set) : SetKWKind, Set by set {

    fun  foldLeft(b: B, f: (B, A) -> B): B = this.fold(b, f)

    fun  foldRight(lb: Eval, f: (A, Eval) -> Eval): Eval {
        fun loop(fa_p: SetKW): Eval = when {
            fa_p.set.isEmpty() -> lb
            else -> f(fa_p.set.first(), Eval.defer { loop(fa_p.set.drop(1).toSet().k()) })
        }
        return Eval.defer { loop(this) }
    }

    companion object {

        fun  pure(a: A): SetKW = setOf(a).k()

        fun  empty(): SetKW = emptySet().k()

    }
}

fun  SetKW.combineK(y: SetKWKind): SetKW = (this.set + y.ev().set).k()

fun  Set.k(): SetKW = SetKW(this)