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

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.

主站蜘蛛池模板: 东乌| 介休市| 闸北区| 互助| 文登市| 怀集县| 汉寿县| 永登县| 门头沟区| 永安市| 阳曲县| 黑龙江省| 长丰县| 平和县| 德惠市| 深州市| 思茅市| 旺苍县| 枝江市| 凌海市| 焉耆| 克东县| 洪洞县| 桓仁| 营山县| 马边| 互助| 崇阳县| 芮城县| 罗江县| 顺昌县| 南皮县| 江陵县| 镇康县| 维西| 西丰县| 尉氏县| 银川市| 宝山区| 陆丰市| 章丘市|