alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (ApplicativeSyntax.scala)

This example Scala source code file (ApplicativeSyntax.scala) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Scala by Example" TM.

Learn more about this Scala project at its project page.

Java - Scala tags/keywords

applicative, applicativeidv, boolean, int, need, ops, toapplicativeops, toapplicativeops0, unapply

The ApplicativeSyntax.scala Scala example source code

package scalaz
package syntax

/** Wraps a value `self` and provides methods related to `Applicative` */
final class ApplicativeOps[F[_],A] private[syntax](val self: F[A])(implicit val F: Applicative[F]) extends Ops[F[A]] {
  ////
  final def unlessM(cond: Boolean): F[Unit] = scalaz.std.boolean.unlessM(cond)(self)
  final def whenM(cond: Boolean): F[Unit] = scalaz.std.boolean.whenM(cond)(self)
  final def replicateM(n: Int): F[List[A]] =
    F.replicateM(n, self)

  final def replicateM_(n: Int): F[Unit] =
    F.replicateM_(n, self)
  ////
}

sealed trait ToApplicativeOps0 {
  implicit def ToApplicativeOpsUnapply[FA](v: FA)(implicit F0: Unapply[Applicative, FA]) =
    new ApplicativeOps[F0.M,F0.A](F0(v))(F0.TC)

}

trait ToApplicativeOps extends ToApplicativeOps0 with ToApplyOps {
  implicit def ToApplicativeOps[F[_],A](v: F[A])(implicit F0: Applicative[F]) =
    new ApplicativeOps[F,A](v)

  ////
  implicit def ApplicativeIdV[A](v: => A) = new ApplicativeIdV[A] {
    private[this] 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)
  }  ////
}

trait ApplicativeSyntax[F[_]] extends ApplySyntax[F] {
  implicit def ToApplicativeOps[A](v: F[A]): ApplicativeOps[F, A] = new ApplicativeOps[F,A](v)(ApplicativeSyntax.this.F)

  def F: Applicative[F]
  ////
  def point[A](a: => A)(implicit F: Applicative[F]): F[A] = F.point(a)

  /** Alias for `point` */
  def pure[A](a: => A)(implicit F: Applicative[F]): F[A] = F.point(a)
  def η[A](a: => A)(implicit F: Applicative[F]): F[A] = F.point(a)

  implicit def ApplicativeIdV[A](v: => A) = new ApplicativeIdV[A] {
    private[this] val vc = Need(v)
    def self = vc.value
  }

  trait ApplicativeIdV[A] extends Ops[A] {
    def point(implicit F: Applicative[F]): F[A] = Applicative[F].point(self)

    /** Alias for `point` */
    def pure(implicit F: Applicative[F]): F[A] = Applicative[F].point(self)

    def η(implicit F: Applicative[F]): F[A] = Applicative[F].point(self)
  }

  ////
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala ApplicativeSyntax.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.