
commonMain.space.kscience.dataforge.actions.Action.kt Maven / Gradle / Ivy
package space.kscience.dataforge.actions
import kotlinx.coroutines.CoroutineScope
import space.kscience.dataforge.data.DataSet
import space.kscience.dataforge.meta.Meta
import space.kscience.dataforge.misc.DFExperimental
/**
* A simple data transformation on a data node. Actions should avoid doing actual dependency evaluation in [execute].
*/
public interface Action {
/**
* Transform the data in the node, producing a new node. By default it is assumed that all calculations are lazy
* so not actual computation is started at this moment.
*
* [scope] context used to compute the initial result, also it is used for updates propagation
*/
public suspend fun execute(dataSet: DataSet, meta: Meta = Meta.EMPTY, scope: CoroutineScope? = null): DataSet
public companion object
}
/**
* Action composition. The result is terminal if one of its parts is terminal
*/
public infix fun Action.then(action: Action): Action {
// TODO introduce composite action and add optimize by adding action to the list
return object : Action {
override suspend fun execute(dataSet: DataSet, meta: Meta, scope: CoroutineScope?): DataSet {
return action.execute([email protected](dataSet, meta, scope), meta, scope)
}
}
}
@DFExperimental
public suspend fun DataSet.transformWith(
action: Action,
meta: Meta = Meta.EMPTY,
scope: CoroutineScope? = null,
): DataSet = action.execute(this, meta, scope)
© 2015 - 2025 Weber Informatics LLC | Privacy Policy