- 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.
- Reporting with Visual Studio and Crystal Reports
- ReSharper Essentials
- Building a Game with Unity and Blender
- 自己動手寫Java虛擬機
- C/C++常用算法手冊(第3版)
- Learning Python Design Patterns(Second Edition)
- Eclipse Plug-in Development:Beginner's Guide(Second Edition)
- Python 3破冰人工智能:從入門到實戰(zhàn)
- Kali Linux Wireless Penetration Testing Beginner's Guide(Third Edition)
- Learning YARN
- BeagleBone Robotic Projects(Second Edition)
- ActionScript 3.0從入門到精通(視頻實戰(zhàn)版)
- Android系統(tǒng)下Java編程詳解
- Go語言從入門到精通
- Mastering jQuery Mobile