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

commonMain.computedvalues.Sums.kt Maven / Gradle / Ivy

There is a newer version: 4.10.0-legacy-ie11
Show newest version
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