我一直在尝试使用函数式编程,而我正试图绕过部分应用程序和currying。
我正在寻找一些有关如何将部分应用和 currying 应用于以下功能的见解:
var Page_Validators = [{
"controltovalidate": "Content_C002_txtAddress",
"focusOnError": "t",
"errormessage": "No Address Entered",
"display": "Dynamic",
"validationGroup": "ProfileControl",
"initialvalue": "",
"isvalid": true
}, {
"controltovalidate": "Content_C002_txtCity",
"focusOnError": "t",
"errormessage": "No City Entered",
"display": "Dynamic",
"validationGroup": "ProfileControl",
"initialvalue": "",
"isvalid": true
}, {
"controltovalidate": "Content_C002_drpState",
"focusOnError": "t",
"errormessage": "State Required",
"display": "Dynamic",
"validationGroup": "ProfileControl",
"initialvalue": "",
"isvalid": true
}, {
"controltovalidate": "Content_C002_txtZipcode",
"focusOnError": "t",
"errormessage": "No Zipcode Entered",
"display": "Dynamic",
"validationGroup": "ProfileControl",
"initialvalue": "",
"isvalid": true
}, {
"controltovalidate": "Content_C002_phoneNumberFull",
"focusOnError": "t",
"errormessage": "Missing Phone Number",
"display": "Dynamic",
"validationGroup": "ProfileControl",
"initialvalue": "",
"isvalid": true
}, {
"controltovalidate": "Content_C002_txtSupFullName",
"focusOnError": "t",
"errormessage": "No Name Entered",
"display": "Dynamic",
"validationGroup": "SupervisorControl",
"initialvalue": "",
"isvalid": true
}, {
"controltovalidate": "Content_C002_txtSupTitle",
"focusOnError": "t",
"errormessage": "No Title Entered",
"display": "Dynamic",
"validationGroup": "SupervisorControl",
"initialvalue": "",
"isvalid": true
}, {
"controltovalidate": "Content_C002_txtSupPhoneFull",
"focusOnError": "t",
"errormessage": "Missing Phone Number",
"display": "Dynamic",
"validationGroup": "SupervisorControl",
"initialvalue": "",
"isvalid": true
}, {
"controltovalidate": "Content_C002_txtSupEmail",
"focusOnError": "t",
"errormessage": "No Email Entered",
"display": "Dynamic",
"validationGroup": "SupervisorControl",
"initialvalue": "",
"isvalid": true
}, {
"controltovalidate": "Content_C002_SignatureField",
"focusOnError": "t",
"errormessage": "Signature Required",
"display": "Dynamic",
"validationGroup": "ProfileControl",
"initialvalue": "",
"isvalid": true
}];
function filterArrayBy(arr, searchString) {
return arr.filter(key => key.controltovalidate.toLowerCase().includes(searchString.toLowerCase()));
}
function toggleValidatorsState(arr, condtion) {
return arr.map(el => ValidatorEnable(el, condition));
}
const supervisorValidators = filterArrayBy(Page_Validators, "txtSup");
const disabledValidators = toggleValidatorsState(supervisorValidators, true);
console.log(disabledValidators);
ASIDE
toggleValidatorState函数正在调用ASP.NET函数,因此代码片段不起作用(除非你有asp.net)
除了ASP.NET函数之外,可以简化这些函数还是使用curry部分应用程序?
我觉得我必须通过将数组传递给两个函数来重复自己。
我一直在研究使用Lodash或Ramda但是如果不使用外部库我可以实现相同的目标。
答案 0 :(得分:2)
这可能是一种过度杀伤,因为代码看起来很简单。
但你可以通过将filterArayBy
绑定到Page_Validators
来讨好disableValidators
。
因此,您可以通过不同的searchStrings对其进行过滤,而无需每次都传递数组。
此外,您可以通过toggleValidatorsState
部分应用condition = true
来制作名为.bind
的新功能。
实现这一目标的最简单方法是使用function filterArrayBy(arr, searchString) {
return arr.filter(key => key.controltovalidate.toLowerCase().includes(searchString.toLowerCase()));
}
// We are going to partially apply filterArrayBy function as the first argument is going to be the same.
// (in this context partial application does the same as currying)
const filterPageValidators = filterArrayBy.bind(null, Page_Validators);
function toggleValidatorsState(arr, condtion) {
return arr.map(el => ValidatorEnable(el, condition));
}
// Also partially applying toggleValidatorsState to make the code more readable
const disableValidators = arr => toggleValidatorsState(arr, true);
// The usage would look like this
const disabledValidators = disableValidators(filterPageValidators('txtSup'));
和这样的简单lambda:
curry
如果您想让它更复杂,可以查看this article实施您自己的filterPageValidators
方法并使用它来制作partialRight
。
然后你需要类似于Ramda的const filterPageValidators = curry(filterArrayBy)(Page_Validators);
const disableValidators = bind_trailing_args(toggleValidatorsState, true);
。因此,您可以使用this answer作为参考。
最后,你的功能看起来像这样:
LocalDate date = LocalDate.of(2018, Month.MAY, 31);
date = date.with(Month.FEBRUARY);
System.out.println(date.getMonth());
System.out.println(date);
但在我看来,对于这个特殊情况来说,这实在太多了。