- Learn Scala Programming
- Slava Schmidt
- 202字
- 2021-06-10 19:35:51
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.
- Python程序設(shè)計(jì)教程(第2版)
- DevOps入門(mén)與實(shí)踐
- Spring Boot+Spring Cloud+Vue+Element項(xiàng)目實(shí)戰(zhàn):手把手教你開(kāi)發(fā)權(quán)限管理系統(tǒng)
- Functional Programming in JavaScript
- 精通Python設(shè)計(jì)模式(第2版)
- Linux C編程:一站式學(xué)習(xí)
- Machine Learning in Java
- 一本書(shū)講透Java線程:原理與實(shí)踐
- Spring Boot+MVC實(shí)戰(zhàn)指南
- Maker基地嘉年華:玩轉(zhuǎn)樂(lè)動(dòng)魔盒學(xué)Scratch
- Sails.js Essentials
- Oracle Data Guard 11gR2 Administration Beginner's Guide
- Getting Started with Windows Server Security
- 三步學(xué)Python
- Unity Certified Programmer:Exam Guide