
上QQ阅读APP看书,第一时间看更新
Scala combinators
In Scala, a combinator is a Higher-Order Function and also a pure function.
As they are pure functions, we can easily compose them together very well as flexible, fine-grained building blocks for constructing larger, more complex programs.
Some of the frequently used Scala combinators are as follows:
- map
- flatMap
- reduce
- fold
- recover
- filter
- fallbackTo
- zip
Like the try...catch block in sequential programming, Future has the Future.recover block for asynchronous programming. If you are new to Scala Futures, please refer to Chapter 3, Asynchronous Programming with Scala:
def recover(pf: PartialFunction[Throwable, T]) (implicit e: ExecutionContext): Future[T]
It is also a Scala combinator. We will discuss some Scala combinators, such as map and flatMap, in the coming sections.