Skip to content

Commit

Permalink
Add applicative syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
notxcain committed Feb 16, 2016
1 parent 6dbd4b0 commit 087accf
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
3 changes: 2 additions & 1 deletion core/src/main/scala/cats/syntax/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package cats
package syntax

trait AllSyntax
extends ApplySyntax
extends ApplicativeSyntax
with ApplySyntax
with BifunctorSyntax
with CartesianSyntax
with CoflatMapSyntax
Expand Down
28 changes: 28 additions & 0 deletions core/src/main/scala/cats/syntax/applicative.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cats
package syntax

trait ApplicativeSyntax1 {
implicit def applicativeSyntaxU[FA](fa: FA)(implicit U: Unapply[Applicative, FA]): Applicative.Ops[U.M, U.A] =
new Applicative.Ops[U.M, U.A] {
val self = U.subst(fa)
val typeClassInstance = U.TC
}
}

trait ApplicativeSyntax extends ApplicativeSyntax1 {
implicit def applicativeSyntax[F[_], A](fa: F[A])(implicit F: Applicative[F]): Applicative.Ops[F, A] =
new Applicative.Ops[F,A] {
val self = fa
val typeClassInstance = F
}
implicit def applicativeIdSyntax[A](a: A): ApplicativeIdOps[A] = new ApplicativeIdOps[A](a)
implicit def applicativeEvalSyntax[A](a: Eval[A]): ApplicativeEvalOps[A] = new ApplicativeEvalOps[A](a)
}

final class ApplicativeIdOps[A](val a: A) extends AnyVal {
def pure[F[_]](implicit F: Applicative[F]): F[A] = F.pure(a)
}

final class ApplicativeEvalOps[A](val a: Eval[A]) extends AnyVal {
def pureEval[F[_]](implicit F: Applicative[F]): F[A] = F.pureEval(a)
}
1 change: 1 addition & 0 deletions core/src/main/scala/cats/syntax/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cats

package object syntax {
object all extends AllSyntax
object applicative extends ApplicativeSyntax
object apply extends ApplySyntax
object bifunctor extends BifunctorSyntax
object cartesian extends CartesianSyntax
Expand Down
8 changes: 8 additions & 0 deletions tests/src/test/scala/cats/tests/SyntaxTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,12 @@ class SyntaxTests extends AllInstances with AllSyntax {
val fz4: F[Z] = (fa |@| fb |@| fc).map(f2)
val fz5: F[Z] = (fa |@| fb |@| fc).apWith(ff2)
}

def testApplicative[F[_]: Applicative, A]: Unit = {
val a = mock[A]
val fa = a.pure[F]

val la = mock[Eval[A]]
val lfa = la.pureEval[F]
}
}

0 comments on commit 087accf

Please sign in to comment.