我已经涉足函数式编程了一点,我正在努力了解部分应用程序和柯里化(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);
放在一边
toggleValidatorState 函数正在调用 ASP.NET 函数,因此代码片段将不起作用(除非您有 asp.net)
除了 ASP.NET 函数之外,这些函数是否可以简化或使用柯里化(Currying)的部分应用程序?
我觉得我必须将一个数组传递给两个函数,这是在重复自己。
我一直在研究使用 Lodash 或 Ramda,但我是否可以在不使用外部库的情况下实现相同的目标。
最佳答案
这可能有点矫枉过正,因为代码看起来已经很简单了。
但你可以 curry filterArayBy
通过将其绑定(bind)到 Page_Validators
.
因此,您可以通过不同的 searchString 对其进行过滤,而无需每次都传递数组。
此外,您可以创建一个名为 disableValidators
的新函数通过部分应用 toggleValidatorsState
与 condition = true
.
实现此目的的最简单方法是使用 .bind
和一个像这样的简单 lambda:
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'));
如果你想让它更复杂,你可以查看this article实现你自己的 curry
方法并用它来制作filterPageValidators
.
然后你需要类似于 Ramda 的 partialRight
的东西.所以你可以使用 this answer作为引用。
最后,您的函数看起来像这样:
const filterPageValidators = curry(filterArrayBy)(Page_Validators);
const disableValidators = bind_trailing_args(toggleValidatorsState, true);
但以我的愚见,对于这个特殊情况来说,这实在是太多了。
关于javascript - 在没有 lodash 的情况下 curry 我的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50617635/