I was thinking a bit and came up with an interesting problem, suppose we have a configuration (input) file with:
我想了一下,想出了一个有趣的问题,假设我们有一个配置(输入)文件:
x -> x + 1
x -> x * 2
x -> x * x
x -> -x
And furthermore we have a list of Integer
s:
此外,我们有一个整数列表:
List<Integer> list = new ArrayList<>();
list.addAll(Arrays.toList(1, 2, 3, 4, 5));
Is there a way to convert the String
s (x -> x + 1
, etc.) to Object
s that represent a lambda expression? Which could then be used as:
有没有办法将字符串(x - > x + 1等)转换为表示lambda表达式的对象?然后可以用作:
Object lambda = getLambdaFromString("x -> x + 1");
if (lambda.getClass().equals(IntFunction.class) {
list.stream().forEach()
.mapToInt(x -> x)
.map(x -> ((IntFunction)lambda).applyAsInt(x))
.forEach(System.out::println);
}
How would I write such a method getLambdaFromString
?
我怎么写这样的方法getLambdaFromString?
- Is there something I could reuse from the JDK/JRE?
- Would I need to write it all by myself?
- Is it possible to narrow down the
Object lambda
to something else that only captures lambdas?
我可以从JDK / JRE中重用一些东西吗?
我需要自己写这些吗?
是否有可能将Object lambda缩小到仅捕获lambda的其他东西?
2 个解决方案
#1
24
Marko's comment on the question is correct. You can't read a bare Java lambda expression out of a file, since such an expression isn't defined without a target type provided by the context. For example, consider the following method declarations:
Marko对这个问题的评论是正确的。您无法从文件中读取裸Java lambda表达式,因为如果没有上下文提供的目标类型,则不会定义此类表达式。例如,请考虑以下方法声明:
void method1(BiFunction<String,String,String> f) { ... }
void method2(BiFunction<Integer,Integer,Integer> f) { ... }
Then in the following code,
然后在下面的代码中,
method1((x, y) -> x + y);
method2((x, y) -> x + y);
the two lambda expressions (x, y) -> x + y
mean completely different things. For method1, the +
operator is string concatenation, but for method2, it means integer addition.
两个lambda表达式(x,y) - > x + y表示完全不同的东西。对于method1,+运算符是字符串连接,但对于method2,它表示整数加法。
This is wandering a bit far afield from your question, but you can read and evaluate a lambda or function expression using a dynamic language. In Java 8 there is the Nashorn JavaScript engine. So instead of attempting to read an evaluate a Java lambda expression, you could read and evaluate a JavaScript function using Nashorn, called from Java.
这在你的问题上有点偏远,但你可以使用动态语言阅读和评估lambda或函数表达式。在Java 8中有Nashorn JavaScript引擎。因此,您可以使用从Java调用的Nashorn来读取和评估JavaScript函数,而不是尝试读取评估Java lambda表达式。
The following code takes a function in arg[0] and applies it to each subsequent, printing the results:
以下代码采用arg [0]中的函数并将其应用于每个后续函数,打印结果:
import java.util.function.Function;
import javax.script.*;
public class ScriptFunction {
public static void main(String[] args) throws Exception {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
@SuppressWarnings("unchecked")
Function<Object,Object> f = (Function<Object,Object>)engine.eval(
String.format("new java.util.function.Function(%s)", args[0]));
for (int i = 1; i < args.length; i++) {
System.out.println(f.apply(args[i]));
}
}
}
For example, running the command
例如,运行命令
java ScriptFunction 'function(x) 3 * x + 1' 17 23 47
gives the results
给出结果
52.0
70.0
142.0
The wrapping of the function string inside of new java.util.function.Function
is necessary in order to create an adapter between Nashorn's notion of a JavaScript function and Java's Function interface. (There might be a better way, but I'm not aware of one.) The cast of the return value of eval
to Function<Object,Object>
results in an unchecked cast warning, which is unavoidable, I think, since this is the boundary between JavaScript, a dynamically-typed language, and Java, which is statically-typed. Finally, no error checking is done. I'm sure this will blow up in a variety of nasty ways if certain assumptions are violated, such as the first argument not actually representing a JavaScript function.
为了在Nashorn的JavaScript函数概念和Java的Function接口之间创建一个适配器,必须在新的java.util.function.Function中包装函数字符串。 (可能有一种更好的方法,但我不知道一种方法。)将函数 ,object>
Still, you might find this technique useful if you have a need to evaluate expressions or functions read from a file.
但是,如果您需要评估从文件中读取的表达式或函数,您可能会发现此技术很有用。
#2
12
I believe that using Nashorn JavaScript engine mentioned in Stuart's answer is the best choice in most cases. However if, for some reason, it's desired to stay within the Java world I have recently created the LambdaFromString library that converts a String code to lambda at runtime.
我相信在大多数情况下,使用Stuart答案中提到的Nashorn JavaScript引擎是最佳选择。但是,如果由于某种原因,我希望保持在Java世界中,我最近创建了LambdaFromString库,它在运行时将String代码转换为lambda。
When using that library the code doing what is specified in the question looks like this:
使用该库时,执行问题中指定的代码如下所示:
List<Integer> list = new ArrayList<>();
list.addAll(Arrays.asList(1, 2, 3, 4, 5));
LambdaFactory lambdaFactory = LambdaFactory.get();
Function<Integer, Integer> lambda = lambdaFactory
.createLambda("x -> x + 1", new TypeReference<Function<Integer, Integer>>() {});
list.stream().map(lambda).forEach(System.out::println); //prints 2 to 6
The only thing that differs is that the type of lambda has to be known and passed to the library so that the compiler knows what "+" means in this context.
唯一不同的是,必须知道lambda的类型并将其传递给库,以便编译器知道在此上下文中“+”的含义。
#1
24
Marko's comment on the question is correct. You can't read a bare Java lambda expression out of a file, since such an expression isn't defined without a target type provided by the context. For example, consider the following method declarations:
Marko对这个问题的评论是正确的。您无法从文件中读取裸Java lambda表达式,因为如果没有上下文提供的目标类型,则不会定义此类表达式。例如,请考虑以下方法声明:
void method1(BiFunction<String,String,String> f) { ... }
void method2(BiFunction<Integer,Integer,Integer> f) { ... }
Then in the following code,
然后在下面的代码中,
method1((x, y) -> x + y);
method2((x, y) -> x + y);
the two lambda expressions (x, y) -> x + y
mean completely different things. For method1, the +
operator is string concatenation, but for method2, it means integer addition.
两个lambda表达式(x,y) - > x + y表示完全不同的东西。对于method1,+运算符是字符串连接,但对于method2,它表示整数加法。
This is wandering a bit far afield from your question, but you can read and evaluate a lambda or function expression using a dynamic language. In Java 8 there is the Nashorn JavaScript engine. So instead of attempting to read an evaluate a Java lambda expression, you could read and evaluate a JavaScript function using Nashorn, called from Java.
这在你的问题上有点偏远,但你可以使用动态语言阅读和评估lambda或函数表达式。在Java 8中有Nashorn JavaScript引擎。因此,您可以使用从Java调用的Nashorn来读取和评估JavaScript函数,而不是尝试读取评估Java lambda表达式。
The following code takes a function in arg[0] and applies it to each subsequent, printing the results:
以下代码采用arg [0]中的函数并将其应用于每个后续函数,打印结果:
import java.util.function.Function;
import javax.script.*;
public class ScriptFunction {
public static void main(String[] args) throws Exception {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
@SuppressWarnings("unchecked")
Function<Object,Object> f = (Function<Object,Object>)engine.eval(
String.format("new java.util.function.Function(%s)", args[0]));
for (int i = 1; i < args.length; i++) {
System.out.println(f.apply(args[i]));
}
}
}
For example, running the command
例如,运行命令
java ScriptFunction 'function(x) 3 * x + 1' 17 23 47
gives the results
给出结果
52.0
70.0
142.0
The wrapping of the function string inside of new java.util.function.Function
is necessary in order to create an adapter between Nashorn's notion of a JavaScript function and Java's Function interface. (There might be a better way, but I'm not aware of one.) The cast of the return value of eval
to Function<Object,Object>
results in an unchecked cast warning, which is unavoidable, I think, since this is the boundary between JavaScript, a dynamically-typed language, and Java, which is statically-typed. Finally, no error checking is done. I'm sure this will blow up in a variety of nasty ways if certain assumptions are violated, such as the first argument not actually representing a JavaScript function.
为了在Nashorn的JavaScript函数概念和Java的Function接口之间创建一个适配器,必须在新的java.util.function.Function中包装函数字符串。 (可能有一种更好的方法,但我不知道一种方法。)将函数 ,object>
Still, you might find this technique useful if you have a need to evaluate expressions or functions read from a file.
但是,如果您需要评估从文件中读取的表达式或函数,您可能会发现此技术很有用。
#2
12
I believe that using Nashorn JavaScript engine mentioned in Stuart's answer is the best choice in most cases. However if, for some reason, it's desired to stay within the Java world I have recently created the LambdaFromString library that converts a String code to lambda at runtime.
我相信在大多数情况下,使用Stuart答案中提到的Nashorn JavaScript引擎是最佳选择。但是,如果由于某种原因,我希望保持在Java世界中,我最近创建了LambdaFromString库,它在运行时将String代码转换为lambda。
When using that library the code doing what is specified in the question looks like this:
使用该库时,执行问题中指定的代码如下所示:
List<Integer> list = new ArrayList<>();
list.addAll(Arrays.asList(1, 2, 3, 4, 5));
LambdaFactory lambdaFactory = LambdaFactory.get();
Function<Integer, Integer> lambda = lambdaFactory
.createLambda("x -> x + 1", new TypeReference<Function<Integer, Integer>>() {});
list.stream().map(lambda).forEach(System.out::println); //prints 2 to 6
The only thing that differs is that the type of lambda has to be known and passed to the library so that the compiler knows what "+" means in this context.
唯一不同的是,必须知道lambda的类型并将其传递给库,以便编译器知道在此上下文中“+”的含义。