xuwei-k / zeroapply   0.5.0

GitHub

zero cost Apply/Applicative syntax

Scala versions: 3.x 2.13 2.12 2.11

zeroapply

Zero Cost Apply/Applicative Syntax

example

zeroapply.OptionApply.apply3(Option(1), Option(2), Option(3))(_ + _ + _)

{
  val x0: _root_.scala.Option[Int] = scala.Option.apply[Int](1);
  if (x0.isEmpty)
    _root_.scala.None
  else
    {
      val x1: _root_.scala.Option[Int] = scala.Option.apply[Int](2);
      if (x1.isEmpty)
        _root_.scala.None
      else
        {
          val x2: _root_.scala.Option[Int] = scala.Option.apply[Int](3);
          if (x2.isEmpty)
            _root_.scala.None
          else
            _root_.scala.Some(x0.get.+(x1.get).+(x2.get))
        }
    }
}
import scalaz._, std.AllInstances._

case class Foo[A, B](a: A, b: B, c: Int)

object Foo {
  implicit def instance[A: Order, B: Order]: Order[Foo[A, B]] =
    zeroapply.CaseClass.order[Foo[A, B]]
}

new Order[Foo[A, B]] {
  override def equalIsNatural =
    Equal[A].equalIsNatural && Equal[B].equalIsNatural && Equal[Int].equalIsNatural

  override def equal(x1: Foo[A, B], x2: Foo[A, B]) =
    Equal[A].equal(x1.a, x2.a) && Equal[B].equal(x1.b, x2.b) && Equal[Int].equal(x1.c, x2.c)

  override def order(x1: Foo[A, B], x2: Foo[A, B]) =
    Order[A].order(x1.a, x2.a) match {
      case Ordering.EQ =>
        Order[B].order(x1.b, x2.b) match {
          case Ordering.EQ => Order[Int].order(x1.c, x2.c)
          case z => z
        }
      case z =>
        z
    }
}

latest stable version

libraryDependencies += "com.github.xuwei-k" %% "zeroapply" % "0.5.0"
libraryDependencies += "com.github.xuwei-k" %% "zeroapply-scalaz" % "0.5.0"

for scalaz 7.2.x

https://github.com/xuwei-k/zeroapply/tree/0.2.x