In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to understand functional programming array.filter tools", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand functional programming array.filter tools.
How lucky we are! Unexpectedly, there is a popular language in the world that almost fully supports FP programming. This language is JavaScript, which ranks first in the list of weapons in StackOverflow over the years.
Functional programming is good for thinking. When we indulge in functional paradigm programming languages such as scheme,lisp, it is a pity that they are absolutely useless in the real world. Only elisp seems to be able to make up for some shortcomings and apply it to emacs, but that's it.
However, how lucky we are! JavaScript almost completely supports the idea of functional programming, and even driven by React, there is a gratifying situation towards FP functional programming, a way to black.
How lucky we are! colleagues of JavaScript.
Where does programming start? Take a look at the name of the second oldest programming language in human history: Lisp, for list processing, the programming language is to deal with list, everything starts with the list array. The task we have to deal with every day is list, our breakfast list is list, and even the goddess has a spare list list. A List can abstract and generalize everything about our human civilization.
Humans realized this, so they named the ancient language lisp,list processing.
Therefore, the road under our feet also starts from list.
Zero, summary of the previous article
In the previous article, we described the core weapon of dealing with arrays, array.reduce. Array.reduce is the Taiji of Taiji, the starting point of all changes, and all other tools can be simulated with array.reduce.
The essence of array.reduce is to reduce the sequence to a value.
Then, based on array.reduce, we deduce array.ma. Array.map applies mathematical ideas directly to programming.
The common characteristics of array.reduce and array.map are the deformation transform processing of array. Next, let's take a look at the parts array.filter and array.find (array.findIndex) that help us think logically.
First, the sieve of array.filter thinking
The core of logical judgment and screening is a keyword predicate. The word pre is disassembled beforehand, while dict is spoken and displayed (for example, dictionary is a dictionary of display words). So predicate logic, that is, say whether the condition is met before executing the code.
The application of array.filter is simple and straightforward, just like the natural flow of our minds:
[1,4,7] .filter (x = > x > 3); / / [4,7] / or Ctrip [1,4,7] .filter (x = > {return x > 3;})
The execution process is shown in the figure:
Second, the application case of array.filter
We began to look at a real-life case of dealing with financial bills stored in JSON:
Const accountBalance = {accountsData: [{id: "user01", balance: 888,}, {id: "user02", balance: 999,}, {id: "user03", balance:-18,}, {id: "user04", balance:-20,}, {id: "user05" Balance: 1,},],}
How to filter out the account information with negative bank assets from the bill? At this time, when we see filter, we are overjoyed, and its application is exactly the same as our thinking process.
Const negativeAccount = accountBalance.accountsData.filter (v = > v.balance)
< 0); console.log(negativeAccount); // 输出结果 //[ { id: 'user03', balance: -18 }, { id: 'user04', balance: -20 } ] 然后,再应用前文的学过的技术,array.map 将负债账户的 ID 提取出来。 const negAccIds = accountBalance.accountsData .filter(v =>V.balance
< 0) .map(v =>V.id); / /: ['user03',' user04'] 3. Simulate array.filter with array.reduce
Looking back to the starting point of everything, array.reduce, he is the source, and all the great rivers originate from reduce. In other words, we always use reduce to think again.
Start to implement array.filter () with reduce simulation:
Const newFilter = (arr, fn) = > arr.reduce ((x, y) = > (fn (y)? X.concat (y): X), [])
After the test, you get the same result:
Const negAccIds = accountBalance.accountsData .newFilter (v = > v.balance
< 0) .map(v =>V.id) / /: ['user03',' user04']
Here, we can also see that the key to applying reduce is to have the accumulator that eventually reduces the dimension to a value in your mind.
Arr.reduce (callback (accumulator, currentValue, [, index [, array]]) [, initialValue]) so far, I believe you have a better understanding of "how to understand functional programming array.filter tools". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.