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

Function object

The orElse method for partial function and curried method for a normal function earlier are just two examples of function-related methods predefined in the standard library.

Similar to the curried method defined for every function instance (except Function0 and Function1) there is another one, tupled, which converts a function of N arguments into the function of one argument which is a TupleN

Besides that, there is a companion object, scala.Function, that incorporates a few methods that are useful for higher order functional programming, most notably a const function, which always returns its argument, and a chain function, which combines a list of functions into a single function as in the following example:

val upper = (_: String).toUpperCase
def fill(c: Char) = c.toString * (_: String).length
def filter(c: Char) = (_: String).filter(_ == c)

val chain = List(upper, filter('L'), fill('*'))
val allAtOnce = Function.chain(chain)

scala> allAtOnce("List(upper, filter('a'), fill('C'))")
res11: String = ****

allAtOnce is a function that is similar to the one that could be constructed by combining our three original functions with andThen (which is defined in the FunctionN trait):

val static = upper andThen filter('a') andThen fill('C')

But allAtOnce is built in a dynamic manner.

主站蜘蛛池模板: 兴文县| 韶山市| 花莲市| 九寨沟县| 察雅县| 通江县| 仁化县| 峡江县| 土默特右旗| 亳州市| 元江| 府谷县| 蒲江县| 宁波市| 合阳县| 通化县| 绥化市| 宁陕县| 鹿泉市| 揭西县| 托里县| 五大连池市| 石棉县| 城步| 光泽县| 富宁县| 富平县| 毕节市| 琼结县| 东兰县| 祁连县| 静宁县| 安陆市| 娄底市| 雷州市| 虹口区| 巴马| 贵定县| 宁阳县| 休宁县| 武清区|