What is Applicative Builder

I am a scala and functional newbie and trying to learn applications.

val o1 = Some(1)
val o2 = Some(2)
val o3 = Some(3)
val result = (o1 |@| o2 |@| o3) {_ + _ + _}

There is a very good understanding of Applicatives and functors here.

Like this blog,

Operator | @ | is a product operation

and

combining your applications into a product with | @ | leads to ApplicativeBuilder, which performs the function to execute on the product (since product + map is a very common use case)

It is very difficult for me to understand the above two statements from the blog. Any example with code in scala to figure this out would be helpful.

+4
source share
1 answer

, , , :

, , , ? , ,

, .

|@|, ApplicativeBuilder - DSL.

( ):

a scalaz.Functor , (ap)


| @|

" " OP , , ApplicativeBuilder.

|@| - , ApplicativeBuilder:

final def |@|[B](fb: F[B]) = new ApplicativeBuilder[F, A, B] {
  val a: F[A] = self
  val b: F[B] = fb
}

F - , Apply:

implicit val F: Apply[F]

Apply - Applicative point.

| @| ApplicativeBuilder, ( product + map - )

Option[Int] s:

import scalaz.Scalaz._

val o1 = 1.some
val o2 = 1.some
val result: ApplicativeBuilder[Option, Int, Int] = o1 |@| o2
val finalRes: Option[Int] = result.apply(_ + _)

  • |@| a Option[Int] ApplicativeBuilder[Option, Int, Int]. Option F, Apply.
  • Apply. Int -> Int Option[Int], , , , .
+3

Source: https://habr.com/ru/post/1685445/


All Articles