Replace T by Pair<Meta, T> in data reducers

This commit is contained in:
Alexander Nozik 2022-05-07 18:06:55 +03:00
parent 0622bacc4d
commit 7d9189e15c
2 changed files with 9 additions and 9 deletions

View File

@ -19,14 +19,14 @@ public class JoinGroup<T : Any, R : Any>(
public var meta: MutableMeta = MutableMeta() public var meta: MutableMeta = MutableMeta()
public lateinit var result: suspend ActionEnv.(Map<Name, T>) -> R public lateinit var result: suspend ActionEnv.(Map<Name, Pair<Meta, T>>) -> R
internal fun <R1 : R> result(outputType: KType, f: suspend ActionEnv.(Map<Name, T>) -> R1) { internal fun <R1 : R> result(outputType: KType, f: suspend ActionEnv.(Map<Name, Pair<Meta, T>>) -> R1) {
this.outputType = outputType this.outputType = outputType
this.result = f; this.result = f;
} }
public inline fun <reified R1 : R> result(noinline f: suspend ActionEnv.(Map<Name, T>) -> R1) { public inline fun <reified R1 : R> result(noinline f: suspend ActionEnv.(Map<Name, Pair<Meta, T>>) -> R1) {
outputType = typeOf<R1>() outputType = typeOf<R1>()
this.result = f; this.result = f;
} }
@ -66,7 +66,7 @@ public class ReduceGroupBuilder<T : Any, R : Any>(
/** /**
* Apply transformation to the whole node * Apply transformation to the whole node
*/ */
public fun result(resultName: String, f: suspend ActionEnv.(Map<Name, T>) -> R) { public fun result(resultName: String, f: suspend ActionEnv.(Map<Name, Pair<Meta, T>>) -> R) {
groupRules += { node -> groupRules += { node ->
listOf(JoinGroup<T, R>(resultName, node, outputType).apply { result(outputType, f) }) listOf(JoinGroup<T, R>(resultName, node, outputType).apply { result(outputType, f) })
} }
@ -82,7 +82,7 @@ internal class ReduceAction<T : Any, R : Any>(
outputType: KType, outputType: KType,
private val action: ReduceGroupBuilder<T, R>.() -> Unit, private val action: ReduceGroupBuilder<T, R>.() -> Unit,
) : CachingAction<T, R>(outputType) { ) : CachingAction<T, R>(outputType) {
//TODO optimize reduction. Currently the whole action recalculates on push //TODO optimize reduction. Currently, the whole action recalculates on push
override fun transform(set: DataSet<T>, meta: Meta, key: Name): Sequence<NamedData<R>> = sequence { override fun transform(set: DataSet<T>, meta: Meta, key: Name): Sequence<NamedData<R>> = sequence {

View File

@ -49,13 +49,13 @@ public inline fun <T1 : Any, T2 : Any, reified R : Any> Data<T1>.combine(
public inline fun <T : Any, reified R : Any> Collection<Data<T>>.reduceToData( public inline fun <T : Any, reified R : Any> Collection<Data<T>>.reduceToData(
coroutineContext: CoroutineContext = EmptyCoroutineContext, coroutineContext: CoroutineContext = EmptyCoroutineContext,
meta: Meta = Meta.EMPTY, meta: Meta = Meta.EMPTY,
crossinline block: suspend (Collection<T>) -> R, crossinline block: suspend (List<Pair<Meta, T>>) -> R,
): Data<R> = Data( ): Data<R> = Data(
meta, meta,
coroutineContext, coroutineContext,
this this
) { ) {
block(map { it.await() }) block(map { it.meta to it.await() })
} }
@DFInternal @DFInternal
@ -63,14 +63,14 @@ public fun <K, T : Any, R : Any> Map<K, Data<T>>.reduceToData(
outputType: KType, outputType: KType,
coroutineContext: CoroutineContext = EmptyCoroutineContext, coroutineContext: CoroutineContext = EmptyCoroutineContext,
meta: Meta = Meta.EMPTY, meta: Meta = Meta.EMPTY,
block: suspend (Map<K, T>) -> R, block: suspend (Map<K, Pair<Meta, T>>) -> R,
): Data<R> = Data( ): Data<R> = Data(
outputType, outputType,
meta, meta,
coroutineContext, coroutineContext,
this.values this.values
) { ) {
block(mapValues { it.value.await() }) block(mapValues { it.value.meta to it.value.await() })
} }