, , .
scalaz.syntax
point, .
point ( haskell return) pure ( ) Applicative. - F, F Applicative.
, .
val
implicit val KA = scalaz.Kleisli.kleisliIdApplicative[Int]
scala Int . , , Reader ( )
import scalaz.std.option.optionInstance
import scalaz.std.list.listInstance
..
, . point .
:
1. :
scalaz.std.option.optionInstance.point("hello")
KA.pure("hello")
2. :
Applicative[Option].point("hello")
,
object Applicative {
@inline def apply[F[_]](implicit F: Applicative[F]): Applicative[F] = F
}
apply Applicative[F] F.
, Applicative[Option].point("hello") Applicative[Option].apply(scalaz.std.option.optionInstance)
optionInstance
3.
import scalaz.syntax.applicative._
:
implicit def ApplicativeIdV[A](v: => A) = new ApplicativeIdV[A] {
val nv = Need(v)
def self = nv.value
}
trait ApplicativeIdV[A] extends Ops[A] {
def point[F[_] : Applicative]: F[A] = Applicative[F].point(self)
def pure[F[_] : Applicative]: F[A] = Applicative[F].point(self)
def η[F[_] : Applicative]: F[A] = Applicative[F].point(self)
} ////
, , point String
"hello".point[Option]
, String point implicits, -, point, String.
, String ApplicativeIdV[String], point:
def point[F[_] : Applicative]: F[A] = Applicative[F].point(self)
, - desugares
new ApplicativeIdV[Option]("hello")
scalaz .
sequence
def sequence[G[_]: Applicative, A](fga: F[G[A]]): G[F[A]] =
traverse(fga)(ga => ga)
G , Applicative[G] .
, :
def sequence[G[_], A](fga: F[G[A]])(implicit ev: Applicative[G[_]]): G[F[A]] =
traverse(fga)(ga => ga)
, , , [G] [F].
import scalaz.std.list.listInstance
import scalaz.std.option.optionInstance
Traverse[List].sequence[Option, String](Option("hello"))