mirror of
https://github.com/DanilaFe/abacus
synced 2024-11-17 16:09:32 -08:00
Convert Reducer interface to Kotlin.
This commit is contained in:
parent
12710c625b
commit
536cac7b23
|
@ -1,19 +0,0 @@
|
|||
package org.nwapw.abacus.tree;
|
||||
|
||||
/**
|
||||
* Interface used to reduce a tree into a single value.
|
||||
*
|
||||
* @param <T> the value to reduce into.
|
||||
*/
|
||||
public interface Reducer<T> {
|
||||
|
||||
/**
|
||||
* Reduces the given tree into a single value of type T.
|
||||
*
|
||||
* @param node the node being passed in to be reduced.
|
||||
* @param children the already-reduced children of this node.
|
||||
* @return the resulting value from the reduce.
|
||||
*/
|
||||
public T reduceNode(TreeNode node, Object... children);
|
||||
|
||||
}
|
|
@ -16,7 +16,7 @@ data class FunctionNode(val function: String) : TreeNode() {
|
|||
val children: MutableList<TreeNode> = mutableListOf()
|
||||
|
||||
override fun <T : Any> reduce(reducer: Reducer<T>): T? {
|
||||
val children = Array<Any?>(children.size, { children[it].reduce(reducer) ?: return null; })
|
||||
val children = Array<Any>(children.size, { children[it].reduce(reducer) ?: return null; })
|
||||
return reducer.reduceNode(this, *children)
|
||||
}
|
||||
|
||||
|
|
19
src/main/kotlin/org/nwapw/abacus/tree/Reducer.kt
Normal file
19
src/main/kotlin/org/nwapw/abacus/tree/Reducer.kt
Normal file
|
@ -0,0 +1,19 @@
|
|||
package org.nwapw.abacus.tree
|
||||
|
||||
/**
|
||||
* Reducer interface that takes a tree and returns a single value.
|
||||
*
|
||||
* The reducer walks the tree, visiting the children first, converting them into
|
||||
* a value, and then attempts to reduce the parent. Eventually, the single final value is returned.
|
||||
*/
|
||||
interface Reducer <out T> {
|
||||
|
||||
/**
|
||||
* Reduces the given tree node, given its already reduced children.
|
||||
*
|
||||
* @param treeNode the tree node to reduce.
|
||||
* @param children the list of children, of type T.
|
||||
*/
|
||||
fun reduceNode(treeNode: TreeNode, vararg children: Any) : T?
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user