commonMain.computedvalues.Sums.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lf-kotlin-js Show documentation
Show all versions of lf-kotlin-js Show documentation
Kotlin integration for Lightweightform
package pt.lightweightform.lfkotlin.computedvalues
import pt.lightweightform.lfkotlin.ComputedValue
import pt.lightweightform.lfkotlin.Context
import pt.lightweightform.lfkotlin.LfLong
import pt.lightweightform.lfkotlin.Path
import pt.lightweightform.lfkotlin.toLfLong
import pt.lightweightform.lfkotlin.toLong
/**
* Computed value with the sum of all integer values produced by the [selector] function applied to
* each element in the array at [dependencyPath].
*/
public open class SumOfInts(
private val dependencyPath: Path,
private val selector: (T) -> Int?
) : ComputedValue {
override fun Context.compute(): Int = get>(dependencyPath).sumOf { selector(it) ?: 0 }
}
/**
* Computed value with the sum of all long values produced by the [selector] function applied to
* each element in the array at [dependencyPath].
*/
public open class SumOfLongs(
private val dependencyPath: Path,
private val selector: (T) -> LfLong?
) : ComputedValue {
override fun Context.compute(): LfLong =
(get>(dependencyPath).sumOf { selector(it)?.toLong() ?: 0 }).toLfLong()
}
/**
* Computed value with the sum of all double values produced by the [selector] function applied to
* each element in the array at [dependencyPath].
*/
public open class SumOfDoubles(
private val dependencyPath: Path,
private val selector: (T) -> Double?
) : ComputedValue {
override fun Context.compute(): Double =
get>(dependencyPath).sumOf { selector(it) ?: 0.0 }
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy