In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the thinking method of Scala programming". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the thinking method of Scala programming".
What kind of programming do you use to think about it? If you use the command method to think about it, you must consider the operation steps such as "first have a variable, then loop and judge such a condition, and then do this operation."
When considering the object-oriented approach, you must first consider "what is in this object in the program (data)?" And then assign an action (method) to the object The overall impression should be that the "objects" assign their own work to each other, and the objects only do their own work and leave the other work to other objects, and the "objects" cooperate with each other to complete a deal. Even in the case of object-oriented, for general languages, object methods are implemented in the form of commands.
So what does functional programming look like? Functional programming is a way of thinking about "applying a function to a value or object," that is, a function simply converts a value or object into something different. In this way, functional methods may still be difficult to emerge in the reader's mind, so let's elaborate on the examples of "imperative" and "functional".
[Scala lecture] topic: apples on the shelf
The content of the topic is to simulate "there are 100 apples on the shelf, one by one peeled and eaten * none." This process. The running result should look like this:
> scala AppleCounter 99 apples on the wall. 98 apples on the wall. : 2 apples on the wall. 1 apple on the wall. No apple on the wall.
When there are only 1 or 0 apples, the treatment is different because it is not plural. Let's do it in a command way.
Object AppleCounter {def main (args: array [string]): Unit = {var appleList:List [String] = List () for (I println (x))}}
First prepare the list variable appleList, and then do 100 loops. In the loop, the variable counter is used to store the current number of apples left, and a description text is added to the end of the example table according to this number, in which special treatment is given for the case of 1 and 0. Use the foreach method of appleList to print each list member. Beside the point, because Scala is a functional + object-oriented language, it's nice to be able to program in this imperative way, which is good for users who are not familiar with functional programming.
All right, let's reimplement the same logic in a functional way this time. So how do you consider the implementation of a function? Here is an example of an implementation.
Object AppleCounter {def main (args: array [string]): Unit = {val appleList = (0 until 100) .acquise.map (x = > x match {case 1 = > "1 apple on the wall." Case 0 = > "no apple on the wall." Case _ = > x + "apples on the wall."}) appleList.foreach (x = > println (x))}}
This is not to say that the program is much shorter, but I hope you understand the difference in your way of thinking (but the type of variable appleList is not declared here, and it's amazing to be able to make such a complex type inference! ). When I wrote this program, I didn't think about the concept of loop in the first place. Instead of looping, I think about how to use functions on numeric list variables. The first thing is not logic, but to create an object to execute the function (in this case, a list of numbers), and then consider which function to choose to execute.
First consider creating the following list object.
List (99, 98, 97,.... 2, 1, 0)
This is done through the (0 until 100) .reverse part. Then for the numbers in this list, consider the function that returns the string corresponding to the number. The corresponding parts are as follows
(list above) .map (x = > x match {case 1 = > "1 apple on the wall." Case 0 = > "no apple on the wall." Case _ = > x + "apples on the wall."})
Map is one of the most important functions when doing decent functional programming with Scala. That is, execute each element in the list with the function passed in by the map function, and then return its list of results. The argument (function object) passed to the map function is: for 1 returns 1 apple on the wall.; and 0 returns no apple on the wall; in other cases such as 22, 22 apples on the wall is returned. Based on this, the map function returns the following list
List ("99 apples on the wall.", "98 apples on the wall.",... "no apple on the wall.")
* one line is similar to imperative programming. For the example table appleList, the foreach method is used to loop through, and the function passed in by foreach is used to print all the elements of the list, and then the program ends.
The function map exists not only in the List class, but also in many other classes.
Scala Lecture Diagram: concept Map of map function
List has powerful functions in functional languages. Again, functional programming is not about how to loop, but about creating a list as an operation source, and then considering what functions should be applied to the elements in the list. Because of the use of such a style of programming, there is no need to save counters or temporary variables for loops.
Thank you for your reading, the above is the content of "what is the thinking method of Scala programming". After the study of this article, I believe you have a deeper understanding of what the thinking method of Scala programming is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.