Skip to content

Commit

Permalink
Added convenience method to get root of an operator tree.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralph Gasser committed Feb 2, 2024
1 parent 4ebb3be commit aafa0c0
Showing 1 changed file with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import kotlinx.coroutines.flow.Flow
* A basic implementation of a pipeline-able operator.
*
* @author Ralph Gasser
* @version 1.0.0
* @version 1.1.0
*/
sealed interface Operator<O> {
/**
* Returns root of this [Unary].
*/
fun root(): Operator<*>

/**
* Converts this [Operator] to a [Flow] of type [O].
Expand All @@ -22,14 +26,24 @@ sealed interface Operator<O> {
/**
* A nullary operator (usually a source).
*/
interface Nullary<O>: Operator<O>
interface Nullary<O>: Operator<O> {
/**
* Returns root of this [Nullary] (which is this).
*/
override fun root(): Operator<*> = this
}

/**
* A unary [Operator] with one input [Operator] and one output [Operator].
*/
interface Unary<I,O>: Operator<O> {
/** The input [Operator]. */
val input: Operator<I>

/**
* Returns root of this [Unary].
*/
override fun root(): Operator<*> = this.input.root()
}

/**
Expand All @@ -41,6 +55,11 @@ sealed interface Operator<O> {

/** The input [Operator]. */
val input2: Operator<I>

/**
* Returns root of this [Unary], which is the left-hand operator (by definition).
*/
override fun root(): Operator<*> = this.input1.root()
}

/**
Expand All @@ -49,6 +68,11 @@ sealed interface Operator<O> {
interface NAry<I,O>: Operator<O> {
/** The input [Operator]s. */
val inputs: List<Operator<I>>

/**
* Returns root of this [Unary], which is the left-hand operator (by definition).
*/
override fun root(): Operator<*> = this.inputs.first().root()
}

/**
Expand All @@ -57,5 +81,10 @@ sealed interface Operator<O> {
interface Sink<I>: Operator<Unit> {
/** The input [Operator]. */
val input: Operator<I>

/**
* Returns root of this [Unary], which is the left-hand operator (by definition).
*/
override fun root(): Operator<*> = this.input.root()
}
}

0 comments on commit aafa0c0

Please sign in to comment.