1
0
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:
Danila Fedorin 2017-08-08 10:11:30 -07:00
parent 12710c625b
commit 536cac7b23
3 changed files with 20 additions and 20 deletions

View File

@ -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);
}

View File

@ -16,7 +16,7 @@ data class FunctionNode(val function: String) : TreeNode() {
val children: MutableList<TreeNode> = mutableListOf() val children: MutableList<TreeNode> = mutableListOf()
override fun <T : Any> reduce(reducer: Reducer<T>): T? { 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) return reducer.reduceNode(this, *children)
} }

View 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?
}