From da1c78945e9771f69b016b7933173a5e2e77fb10 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 25 Aug 2017 15:25:46 -0700 Subject: [PATCH] Move the code for applicables that require a Reducer. --- .../abacus/function/ReducerApplicable.java | 44 +++++++++++++++++++ .../abacus/function/TreeValueFunction.java | 43 ------------------ .../abacus/function/TreeValueFunction.kt | 12 +++++ 3 files changed, 56 insertions(+), 43 deletions(-) create mode 100644 core/src/main/java/org/nwapw/abacus/function/ReducerApplicable.java delete mode 100644 core/src/main/java/org/nwapw/abacus/function/TreeValueFunction.java create mode 100644 core/src/main/kotlin/org/nwapw/abacus/function/TreeValueFunction.kt diff --git a/core/src/main/java/org/nwapw/abacus/function/ReducerApplicable.java b/core/src/main/java/org/nwapw/abacus/function/ReducerApplicable.java new file mode 100644 index 0000000..7713404 --- /dev/null +++ b/core/src/main/java/org/nwapw/abacus/function/ReducerApplicable.java @@ -0,0 +1,44 @@ +package org.nwapw.abacus.function; + +import org.nwapw.abacus.tree.Reducer; + +/** + * A slightly more specific Applicable that requires a reducer + * to be passed to it along with the parameters. + * @param the type of the input arguments. + * @param the return type of the application. + * @param the required type of the reducer. + */ +public abstract class ReducerApplicable extends Applicable { + + @Override + protected final O applyInternal(T[] params) { + return null; + } + + @Override + public final O apply(T... params) { + return null; + } + + /** + * Applies this applicable to the given arguments, and reducer. + * @param reducer the reducer to use in the application. + * @param args the arguments to apply to. + * @return the result of the application. + */ + public abstract O applyWithReducerInternal(Reducer reducer, T[] args); + + /** + * Applies this applicable to the given arguments, and reducer, + * if the arguments and reducer are compatible with this applicable. + * @param reducer the reducer to use in the application. + * @param args the arguments to apply to. + * @return the result of the application, or null if the arguments are incompatible. + */ + public O applyWithReducer(Reducer reducer, T...args) { + if(!matchesParams(args) || reducer == null) return null; + return applyWithReducerInternal(reducer, args); + } + +} diff --git a/core/src/main/java/org/nwapw/abacus/function/TreeValueFunction.java b/core/src/main/java/org/nwapw/abacus/function/TreeValueFunction.java deleted file mode 100644 index a34cecc..0000000 --- a/core/src/main/java/org/nwapw/abacus/function/TreeValueFunction.java +++ /dev/null @@ -1,43 +0,0 @@ -package org.nwapw.abacus.function; - -import org.nwapw.abacus.number.NumberInterface; -import org.nwapw.abacus.tree.Reducer; -import org.nwapw.abacus.tree.TreeNode; - -/** - * A function that operates on parse tree nodes instead of on already simplified numbers. - * Despite this, it returns a number, not a tree. - */ -public abstract class TreeValueFunction extends Applicable { - - @Override - protected NumberInterface applyInternal(TreeNode[] params) { - return null; - } - - @Override - public NumberInterface apply(TreeNode... params) { - return null; - } - - /** - * Applies the tree value functions to the given tree nodes, - * using the given reducer. - * @param reducer the reducer to use. - * @param params the parameters to apply to. - * @return the result of the application. - */ - public abstract NumberInterface applyWithReducerInternal(Reducer reducer, TreeNode[] params); - - /** - * Checks if the given parameters and reducer can be used - * with this function, and if so, calls the function on them. - * @param reducer the reducer to use. - * @param params the parameters to apply to. - * @return the result of the application, or null if the parameters are incompatible. - */ - public NumberInterface applyWithReducer(Reducer reducer, TreeNode... params) { - if(!matchesParams(params) || reducer == null) return null; - return applyWithReducerInternal(reducer, params); - } -} diff --git a/core/src/main/kotlin/org/nwapw/abacus/function/TreeValueFunction.kt b/core/src/main/kotlin/org/nwapw/abacus/function/TreeValueFunction.kt new file mode 100644 index 0000000..2038e6b --- /dev/null +++ b/core/src/main/kotlin/org/nwapw/abacus/function/TreeValueFunction.kt @@ -0,0 +1,12 @@ +package org.nwapw.abacus.function + +import org.nwapw.abacus.number.NumberInterface +import org.nwapw.abacus.tree.TreeNode + +/** + * A function that operates on trees. + * + * A function that operates on parse tree nodes instead of on already simplified numbers. + * Despite this, it returns a number, not a tree. + */ +abstract class TreeValueFunction : ReducerApplicable()