javascript - 在没有 lodash 的情况下 curry 我的函数

标签 javascript functional-programming

我已经涉足函数式编程了一点,我正在努力了解部分应用程序和柯里化(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 的新函数通过部分应用 toggleValidatorsStatecondition = 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/

相关文章:

JavaScript(道场): Is event-listener removed when object is destroyed

javascript - 检查是否按下任何键 Javascript 没有表单

haskell - J/K/APL 如何按照通用范式进行分类?

java - 函数和谓词参数不明确?

javascript - 如何在 Node.js 中获取 POSTed (jquery) 数组数据(使用 express)

javascript - Angular 路由配置在 1.2.3 中不起作用

javascript - Jquery Modal 点击后只打开一次,如何解决这个问题?

f# - 为什么 [2..3..10] 会被解释为 [2;5;8]

Java 函数式编程 : How to convert a if-else ladder inside for loop to functional style?

相当于 Clojure 的 "reductions"或 python 的 itertools.accumulate 的 Javascript