gpt4 book ai didi

Java 8 Composition,Currying 速记

转载 作者:行者123 更新时间:2023-11-30 07:41:33 25 4
gpt4 key购买 nike

我有一个 BusinessObject 的流s,我需要给每个对象设置一个值,我想用Stream.map但是map需要 Function<T,R>我有目标对象、鉴别器值和新值。 setNewValueInBusinessObjectExample展示了我想做的事和setNewValueInBusinessObjectWithFun这是我需要帮助的。

同意!我可以使用 setNewValueInBusinessObjectExample在 map 上,但我想看看功能样式如何。谢谢

class BusinessObject {
String firstField;
String secondField;
}

class SomeDiscriminator {
String value;
}

BusinessObject setNewValueInBusinessObjectExample(BusinessObject businessObject,
SomeDiscriminator discriminator, String newValue) {

if(discriminator.value.equals("firstField")) {
businessObject.firstField = newValue;
} else {//secondField
businessObject.secondField = newValue;
}

return businessObject;
}

Function<BusinessObject, Function<SomeDiscriminator, Function<String, BusinessObject>>>
setNewValueInBusinessObjectWithFun = {
/* todo: using nested Function<T,R> */
}

最佳答案

如果您对函数式接口(interface)的构造和使用有疑问,我建议您将整个事情扩展到结构变得明显的匿名类。

我还注意到整个流程使用三个参数,与您的 setNewValueInBusinessObjectExample 一样。从而将方法体移动到最里面的匿名类。

Function<BusinessObject, Function<SomeDiscriminator, Function<String, BusinessObject>>> setNewValueInBusinessObjectWithFun =
new Function<BusinessObject, Function<SomeDiscriminator, Function<String, BusinessObject>>>() {
@Override
public Function<SomeDiscriminator, Function<String, BusinessObject>> apply(final BusinessObject businessObject) {
return new Function<SomeDiscriminator, Function<String, BusinessObject>>() {
@Override
public Function<String, BusinessObject> apply(final SomeDiscriminator someDiscriminator) {
return new Function<String, BusinessObject>() {
@Override
public BusinessObject apply(final String newValue) {
if (someDiscriminator.value.equals("firstField")) {
businessObject.firstField = newValue;
} else {//secondField
businessObject.secondField = newValue;
}
return businessObject;
}
};
}
};
}
};

现在,将所有内容打包到 lambda 表达式中,看看会发生什么:

Function<BusinessObject, Function<SomeDiscriminator, Function<String, BusinessObject>>> setNewValueInBusinessObjectWithFun = 
businessObject -> someDiscriminator -> newValue -> {
if (someDiscriminator.value.equals("firstField")) {
businessObject.firstField = newValue;
} else {//secondField
businessObject.secondField = newValue;
}
return businessObject;
};

为了清楚起见,请正确命名 lambda 表达式中的变量,否则您将无法很好地使用它们。用法相当简单(为了简洁起见,我将 setter 移到了构造函数中:

BusinessObject businessObject = new BusinessObject("oldValue");
setNewValueInBusinessObjectWithFun
.apply(businessObject) // apply to an object
.apply(new SomeDiscriminator("firstField")) // finds its field
.apply("newValue"); // sets a new value

但是,我建议您使用更直接的定义来定义自定义 @FunctionalInterface...

@FunctionalInterface
interface MyFunction<T, R, U> {
T apply(T t, R r, U u);
}

...和用法...

MyFunction<BusinessObject, SomeDiscriminator, String> myFunction = 
(businessObject, someDiscriminator, newValue) -> {
if (someDiscriminator.value.equals("firstField")) {
businessObject.firstField = newValue;
} else {
businessObject.secondField = newValue;
}
return businessObject;
};

BusinessObject businessObject = new BusinessObject("oldValue");
myFunction.apply(businessObject, new SomeDiscriminator("firstField"), "newValue");

关于Java 8 Composition,Currying 速记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55916596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com