In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you the example analysis of kotlin closures, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Closure, functional programming gospel
Learn about functional programming (Functional Programming) first
Concept: it belongs to a kind of "structured programming". The main idea is to write the operation as a series of nested function calls as much as possible. The most important foundation of a functional programming language is the λ operation (Lambda expression). The function of the λ operation can accept the function as a parameter or return value.
Comparison between functional programming and object-oriented programming
Object-oriented programming (Object-oriented programming, abbreviated OOP)
Object-oriented programming is a programming paradigm with the concept of object, which may contain data, attributes and methods. It takes the object as the basic unit of the program and encapsulates the method and data in it in order to improve the reusability, flexibility and expansibility of the software. The program in the object can access and often modify the data associated with the object. In object-oriented programming, computer programs are designed to be related to each other.
Advantages of object-oriented programming 1. The structure of the program
Object-oriented programming can be regarded as an idea that contains all kinds of independent and mutually calling objects in a program. Compared with the traditional procedure-oriented programming, which regards a program as a collection of a series of functions, which is not systematic and structured, object-oriented programming structures a series of related data and methods, encapsulates them into classes, and invokes methods and properties through the objects of the class. It makes it easier for programmers to analyze, design, and understand.
two。 Flexibility and maintainability of programs
Due to the characteristics of integration, encapsulation and polymorphism, object-oriented programming can better design a system structure with high cohesion and low coupling, which makes the system more flexible, easier to expand, and lower development and maintenance costs.
Disadvantages of object-oriented programming
The operation efficiency is low
Although the development efficiency of object-oriented is high, the running efficiency of code is much lower than that of process-oriented, which limits that object-oriented usage scenarios can not include those areas that are very demanding on performance.
Multithreaded data is not safe
Object-oriented programming takes data as the core, so in multi-thread concurrent programming, when multiple threads operate data at the same time, it may lead to the uncertainty of data modification.
The advantages of functional programming (it can be said to be designed to solve the shortcomings of object-oriented)
Thread safety
In functional programming, the data are all immutable, so there is no problem of concurrent programming, it is multi-thread safe, and can effectively reduce the side effects of the program. For fast iterative projects, functional programming can realize the hot switch between functions without worrying about the data, because it takes the function as the minimum unit. as long as the relationship between the function and the function is correct, the correctness of the result can be guaranteed.
The code is highly readable
The expression of functional programming is more in line with the syntax of human daily life, and the code is more readable. The code required to achieve the same functional programming is much less than that of object-oriented programming, and the code is more concise and clear.
The disadvantages of functional programming
Run more slowly
Because all the data is immutable, all the variables always exist during the running of the program, which takes up a lot of running resources. At the same time, due to the congenital design of the function, the performance has not been enough. Although modern sweat programming languages use many techniques, such as lazy computing, to optimize the running speed, they can never be compared with object-oriented, and certainly slower than process-oriented programs.
After learning about functional programming, let's go back to today's topic-closures.
What is a closure?
As we all know, program variables are divided into global variables and local variables, global variables, as the name implies, its scope is the current file or even all the places outside the file, while local variables can only be obtained in its limited scope.
So, how do you call local variables externally? The answer is closure, which defines a closure: a closure is a function that can read internal variables of other functions.
It's the running environment.
It holds the running state of the function
Its interior can define functions.
Its interior can also define classes.
First of all, let's take a simple example.
/ / this is a high-order function fun makeFun (): ()-> Unit {var conut = 0 return fun () {/ / returns an anonymous function, which holds the state of count println (+ + conut)}} fun main () {val makeFun = makeFun () / / function call, and returns a function makeFun () / / to call the returned function. At this point, makeFun holds the state of makeFun () internal variable makeFun () makeFun ()}
Running result:
In a slightly more complex example, for example, implement the Fibonacci sequence
/ / Fibonacci sequence fun fibonacci (): ()-> Long {var first = 0L var second = 1L return fun (): Long {/ / returns the function val result = second second + = first first = second-first return result}} fun main () {val fibo = fibonacci () / at this time The returned function fibo holds the state of the variables inside the fibonnacci () function println (fibo ())}
Test run:
Using iterator to realize Fibonacci sequence
/ / use iterator to implement Fibonacci sequence (instead of returning a function but an object here) fun fibonacci2 (): Iterable {var first = 0L var second = 1L return Iterable {object: LongIterator () {override fun hasNext () = true override fun nextLong (): Long {val result = second second + = first First = second-first return result} fun main () {val fibo2 = fibonacci2 () for (I in fibo2) {if (I > 60) break println (I)}}
Running result:
The above is all the contents of the article "sample Analysis of kotlin closures". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.