void Foo(int a, int b)
=> Bar(a + 1, b + 1)
这可以这样写,以便对Inc
进行单一定义void Foo(int a, int b)
{
Func<int, int> Inc = (x) => x + 1;
Bar(Inc(a), Inc(b));
}
我正在寻找优雅的方法来保持表达式Foo的定义,但只有一个lambda来定义Inc表达式。如果我将Inc作为分离函数很容易,但我有兴趣将它保持为lambda。
答案 0 :(得分:-2)
这将起作用,但缺点是必须为每个参数编写,但作为Func&lt;&gt;以类似的方式定义它并不是那么糟糕。
Element root = document.getRootElement();
String id = null;
boolean check = false;
String idRef = null;
ElementFilter filter = new org.jdom2.filter.ElementFilter(
"DATA-DECLARATION");
ElementFilter filter2 = new org.jdom2.filter.ElementFilter(
"DATA-DECLARATION-REF");
for (Element dataDecId : root.getDescendants(filter))
{
check = false;
id = dataDecId.getAttributeValue("ID");
for (Element dataDecIdRef : root.getDescendants(filter2))
{
idRef = dataDecIdRef.getAttributeValue("ID-REF");
if (null != idRef && idRef.equalsIgnoreCase(id))
{
check = true;
break;
}
}
if (!check)
{
root.removeContent(dataDecId);
}
}
可以像这样使用:
public static class FunctionalExtensions
{
public static Action<A, A> ArgumentTransform<A>(this Func<A, A> argConverter, Action<A, A> action)
=> (x, y) => action(argConverter(x), argConverter(y));
public static Func<A, A, R> ArgumentTransform<A, R>(this Func<A, A> argConverter, Func<A, A, R> function)
=> (x, y) => function(argConverter(x), argConverter(y));
}