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

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.

主站蜘蛛池模板: 井研县| 舒兰市| 许昌县| 璧山县| 犍为县| 武冈市| 信丰县| 长治市| 剑川县| 滕州市| 吉水县| 武义县| 新平| 周至县| 潞西市| 太仓市| 雷山县| 兴城市| 迁安市| 惠东县| 武威市| 东山县| 平谷区| 淮北市| 汉源县| 怀仁县| 沂水县| 班戈县| 伊春市| 新宁县| 铜鼓县| 镇平县| 天全县| 南开区| 石景山区| 白水县| 汉寿县| 连平县| 福鼎市| 开封县| 肇东市|