- Learn Scala Programming
- Slava Schmidt
- 202字
- 2021-06-10 19:35:51
Summary
Functions represent another side of the blend of object-oriented and functional features in Scala. They can be defined in a number of ways, including the partial application of methods, function literals, and partial functions. Functions can be defined in any scope. If a function closes over variables available in scope, it is called closure.
Polymorphic functions implement an idea similar to polymorphism in object orientation, but apply that idea for types of parameters and of the result. This is called parametric polymorphism. It is especially useful when defining functions accepting other functions as arguments, so-called higher order functions.
There are two ways to implement recursion and only tail-recursive functions are stack safe in the JVM. For the functions which cannot be made tail-recursive, there is a way to represent the call chain in the heap by encoding it as objects. This approach is called trampolining and it is supported in the standard library.
Functions are first-class values in Scala because they are implemented as anonymous classes extending FunctionN traits. This not only makes it possible to work with functions as with normal variables, but it also allows for the provision of extended function implementations with additional properties, for example, a PartialFunction.
- C程序設計簡明教程(第二版)
- Selenium Design Patterns and Best Practices
- Java程序設計與計算思維
- JavaScript by Example
- Getting Started with SQL Server 2012 Cube Development
- Yocto for Raspberry Pi
- 從Excel到Python:用Python輕松處理Excel數據(第2版)
- Building RESTful Python Web Services
- Kotlin開發教程(全2冊)
- Scala Data Analysis Cookbook
- ArcGIS for Desktop Cookbook
- MINECRAFT編程:使用Python語言玩轉我的世界
- R語言數據可視化:科技圖表繪制
- 深入實踐DDD:以DSL驅動復雜軟件開發
- Keil Cx51 V7.0單片機高級語言編程與μVision2應用實踐