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/02 Report--
This article introduces the relevant knowledge of "what is the method of Scala to simplify code". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Another important application of higher-order functions is to put them in API to make the client code more concise. The purpose-specific loop method of Scala's collection type provides a good example. These specific-purpose loop methods are defined in idiosyncratic Iterable, extended by List,Set,Array, and Map. For now, however, notice one of the examples to see why these methods are so useful.
Consider exists, a method to determine whether the incoming value is included in the collection. Of course, you can also initialize a var to false, loop through the collection type, check each element, and set var to true if you find what you are looking for. The following is an example of using this method to determine whether the incoming List contains a negative number:
Def containsNeg (nums: List [Int]): Boolean = {var exists = false for (num)
< - nums) if (num < 0) exists = true exists } 假如你在解释器里定义了这个方法,你就可以这样调用: scala>ContainsNeg (List (1,2,3,4) res0: Boolean = false scala > containsNeg (List (1,2,3,4)) res1: Boolean = true
However, a more concise way to define this method is by calling the higher-order function exists on the incoming List, such as:
Def containsNeg (nums: List [Int]) = nums.exists (_
< 0) 这个版本的containsNeg能产生和前面的那个一样的结果: scala>ContainsNeg (Nil) res2: Boolean = false scala > containsNeg (List (0,1,-2)) res3: Boolean = true
The exists method represents the control abstraction. It is the purpose-specific loop architecture provided by the Scala library rather than built into the Scala language like while or for. In the previous section, the higher-order function, filesMatching, reduces code duplication in the implementation of the object FileMatcher. The exists method provides a similar benefit, but because exists is exposed in Scala's collection type API, it reduces duplication in API's client code. If exists doesn't exist, if you want to write a containsOdd method to check if the list contains odd numbers, you might write something like this:
Def containsOdd (nums: List [Int]): Boolean = {var exists = false for (num <-nums) if (num% 2 = = 1) exists = true exists}
If you compare the function bodies of containsNeg and containsOdd, you will find that everything except the if expression is duplicated. Using exists, you can write:
Def containsOdd (nums: List [Int]) = nums.exists (_% 2 = = 1)
Once again, this version of the code body is consistent with the corresponding containsNeg method (using the exists version), except for different search conditions. However, the amount of code repetition is much less, because all the loop schemas are extracted into the exists method itself.
There are many other loop methods in the standard library of Scala. If you can find opportunities to use them, then, like exists, they can often shorten your code.
This is the end of the content of "what is the way for Scala to simplify the code". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.