簡體   English   中英

咖喱我的功能沒有lodash

[英]Curry my Functions without lodash

我已經開始涉足函數式編程了一段時間,而我正在努力使自己了解部分應用程序和應用程序。

我在找我如何可以申請部分應用程序鑽營下面的功能的一些見解:

 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功能外,這些功能是否可以簡化或使用部分應用程序?

我覺得我不得不通過將數組傳遞給兩個函數來重復自己。

我一直在研究使用Lodash或Ramda,但如果不使用外部庫就可以達到相同的效果。

這在這里可能是一個過大的殺手,因為代碼已經看起來很簡單。

但是您可以通過將filterArayBy綁定到Page_ValidatorsPage_Validators 因此,您可以通過不同的searchStrings對其進行過濾,而無需每次都傳遞該數組。

另外,您可以通過部分使用condition = true toggleValidatorsState來創建一個名為disableValidators的新函數。

實現此目的的最簡單方法是使用.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')); 

如果您想使其更復雜,可以查看本文實現自己的curry方法並使用它來制作filterPageValidators

然后,您將需要類似於partialRightpartialRightpartialRight 因此,您可以將此答案用作參考。

最后,您的函數看起來像這樣:

 const filterPageValidators = curry(filterArrayBy)(Page_Validators); const disableValidators = bind_trailing_args(toggleValidatorsState, true); 

但是,以我的拙見,對於這種特殊情況,這確實太多了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM