Applicative
|
Term |
applicative |
|
Aliases |
applicative functor |
Definition
An applicative is an abstraction that allows applying a function in a context to a value in a context. It provides a way to combine independent computations via operations like pure , ap , or mapN , enabling parallel or independent evaluation.
Scala
Catsにおける型クラス Applicative の定義は次のようになります。
trait Applicative[F[_]] extends Apply[F] {
def pure[A](x: A): F[A]
}
ここで、 Apply は ap および map2 などの組み合わせ演算を提供し、 Applicative はそれに pure を加えることで値の持ち上げを可能にします。
さらに、 Applicative の代表的なユーティリティとして mapN シンタックスがあります:
import cats.implicits._
(pure(a), pure(b), pure(c)).mapN { (x, y, z) =>
combine(x, y, z)
}
このようにして、依存しない複数の文脈付き値を同時に処理する記法を簡潔に記述できます。