官术网_书友最值得收藏!

  • 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.

主站蜘蛛池模板: 丰县| 通辽市| 合江县| 章丘市| 呼伦贝尔市| 宣城市| 洞头县| 于田县| 开平市| 汪清县| 茂名市| 贺兰县| 西峡县| 孟连| 吉林市| 武穴市| 巴林左旗| 新龙县| 北辰区| 平利县| 安溪县| 老河口市| 灵武市| 内丘县| 兴隆县| 蓬莱市| 蕲春县| 吉木乃县| 阳新县| 石渠县| 广东省| 神木县| 宁河县| 清涧县| 当阳市| 乐山市| 潮安县| 恩施市| 白山市| 松阳县| 富蕴县|