Skip to content

Commit

Permalink
Merge pull request #56 from kailuowang/remove-monad
Browse files Browse the repository at this point in the history
RC0 changes
  • Loading branch information
kailuowang authored Sep 6, 2017
2 parents 3b565e2 + 98b2e50 commit d898a33
Show file tree
Hide file tree
Showing 37 changed files with 266 additions and 610 deletions.
34 changes: 25 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,38 @@ libraryDependencies += "org.typelevel" %% "kittens" % "1.0.0-M11"

### Auto derived Examples



```scala

scala> import cats.derived._, functor._, legacy._
scala> import cats.Functor
scala> import cats.implicit._, cats._

scala> case class Cat[Food](food: Option[Food], foods: List[Food])
scala> case class Cat[Food](food: Food, foods: List[Food])
defined class Cat

scala> val f = Functor[Cat]
f: cats.Functor[Cat] = cats.derived.MkFunctor2$$anon$4@782b2ad1
scala> val cat = Cat(1, List(2, 3))
cat: Cat[Int] = Cat(1,List(2, 3))

```

#### Derive `Functor`

scala> val cat = Cat(Some(1), List(2, 3))
cat: Cat[Int] = Cat(Some(1),List(2, 3))
```scala
scala> implicit val fc = cats.derive.functor[Cat]
FC: cats.Functor[Cat] = cats.derived.MkFunctor2$$anon$4@1c60573f

scala> cat.map(_ + 1)
res0: Cat[Int] = Cat(2,List(3, 4))
```

#### Derive `Show`

```scala
scala> implicit val cs = cats.derive.show[Cat[Int]]
catShow: cats.Show[Cat[Int]] = cats.derived.MkShow3$$anon$1@7ec30e6b

scala> f.map(cat)(_ + 1)
res3: Cat[Int] = Cat(Some(2),List(3, 4))
scala> cat.show
res1: String = Cat(food = 1, foods = List(2, 3))
```

### Sequence examples
Expand Down
19 changes: 6 additions & 13 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import sbt._

lazy val buildSettings = Seq(
organization := "org.typelevel",
scalaVersion := "2.12.2",
scalaVersion := "2.12.3",
crossScalaVersions := Seq( "2.11.11", scalaVersion.value)
)

Expand All @@ -26,7 +26,6 @@ lazy val commonSettings = Seq(
"org.typelevel" %% "cats-core" % "1.0.0-MF",
"org.typelevel" %% "alleycats-core" % "0.2.0",
"com.chuusai" %% "shapeless" % "2.3.2",
"org.typelevel" %% "export-hook" % "1.2.0",
"org.scalatest" %% "scalatest" % "3.0.3" % "test",
"org.scalacheck" %% "scalacheck" % "1.13.5" % "test",
"org.typelevel" %% "cats-laws" % "1.0.0-MF" % "test",
Expand Down Expand Up @@ -54,8 +53,8 @@ lazy val commonJvmSettings = Seq(
lazy val coreSettings = buildSettings ++ commonSettings ++ publishSettings ++ releaseSettings

lazy val root = project.in(file("."))
.aggregate(coreJS, coreJVM, extraTests)
.dependsOn(coreJS, coreJVM, extraTests)
.aggregate(coreJS, coreJVM)
.dependsOn(coreJS, coreJVM)
.settings(coreSettings:_*)
.settings(noPublishSettings)

Expand All @@ -65,12 +64,6 @@ lazy val core = crossProject.crossType(CrossType.Pure)
.jsSettings(commonJsSettings:_*)
.jvmSettings(commonJvmSettings:_*)

//Monad and Applicative tests are taking a long time to compile, separating them to another module to help development, and scala 2.10.x build on travis.
lazy val extraTests = project.in(file("extra-tests"))
.settings(moduleName := "kittens-tests")
.dependsOn(coreJVM % "compile->compile;test->test")
.settings(coreSettings:_*)
.settings(noPublishSettings)

lazy val coreJVM = core.jvm
lazy val coreJS = core.js
Expand Down Expand Up @@ -103,9 +96,9 @@ lazy val publishSettings = Seq(
publishMavenStyle := true,
publishArtifact in Test := false,
pomIncludeRepository := { _ => false },
publishTo <<= version { (v: String) =>
publishTo := {
val nexus = "https://oss.sonatype.org/"
if (v.trim.endsWith("SNAPSHOT"))
if (version.value.trim.endsWith("SNAPSHOT"))
Some("snapshots" at nexus + "content/repositories/snapshots")
else
Some("releases" at nexus + "service/local/staging/deploy/maven2")
Expand Down Expand Up @@ -141,7 +134,7 @@ lazy val releaseSettings = Seq(
publishArtifacts,
setNextVersion,
commitNextVersion,
ReleaseStep(action = Command.process("sonatypeReleaseAll", _), enableCrossBuild = true),
ReleaseStep(action = Command.process("sonatypeReleaseAll", _)),
pushChanges
)
)
Expand Down
19 changes: 19 additions & 0 deletions core/src/main/scala/cats/derive.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cats
import alleycats.{EmptyK, Pure}
import cats.derived._

object derive {
def functor[F[_]](implicit F: MkFunctor[F]) : Functor[F] = F
def emptyK[F[_]](implicit F: MkEmptyK[F]): EmptyK[F] = F
def eq[T](implicit F: MkEq[T]): Eq[T] = F
def foldable[F[_]](implicit F: MkFoldable[F]): Foldable[F] = F
def monoid[T](implicit T: MkMonoid[T]): Monoid[T] = T
def monoidK[F[_]](implicit F: MkMonoidK[F]): MonoidK[F] = F
def pure[F[_]](implicit F: MkPure[F]): Pure[F] = F
def semigroup[T](implicit F: MkSemigroup[T]): Semigroup[T] = F
def semigroupK[F[_]](implicit F: MkSemigroupK[F]): SemigroupK[F] = F
def show[T](implicit F: MkShow[T]): Show[T] = F

def iterable[F[_], A](fa: F[A])(implicit mif: MkIterable[F]): Iterable[A] = mif.iterable(fa)

}
18 changes: 0 additions & 18 deletions core/src/main/scala/cats/derived/applicative.scala

This file was deleted.

55 changes: 0 additions & 55 deletions core/src/main/scala/cats/derived/apply.scala

This file was deleted.

7 changes: 3 additions & 4 deletions core/src/main/scala/cats/derived/consk.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@
package cats.derived

import alleycats.ConsK
import export.ExportGeneric
import shapeless._

object consk {
object exports {
def apply[F[_]](implicit mcff: MkConsK[F, F]): ExportGeneric[ConsK[F]] =
ExportGeneric[ConsK[F]](
def apply[F[_]](implicit mcff: MkConsK[F, F]) =
ConsK[F](
new ConsK[F] {
def cons[A](hd: A, tl: F[A]): F[A] = mcff.cons(hd, tl)
}
)

implicit def deriveConsK[F[_]](implicit mcff: MkConsK[F, F]): ExportGeneric[ConsK[F]] = apply[F]
implicit def deriveConsK[F[_]](implicit mcff: MkConsK[F, F]): ConsK[F] = apply[F]
}
}

Expand Down
19 changes: 7 additions & 12 deletions core/src/main/scala/cats/derived/empty.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,27 @@
package cats.derived

import alleycats.Empty
import export.{ exports, imports, reexports }
import shapeless._

@reexports[MkEmpty]
object empty {
@imports[Empty]
object legacy
}
import shapeless._

trait MkEmpty[T] extends Empty[T]

@exports
object MkEmpty {
object MkEmpty extends MkEmptyDerivation {
def apply[T](implicit e: MkEmpty[T]): MkEmpty[T] = e
}

implicit val hnil: MkEmpty[HNil] =
trait MkEmptyDerivation {
implicit val mkEmptyHnil: MkEmpty[HNil] =
new MkEmpty[HNil] {
def empty = HNil
}

implicit def hcons[H, T <: HList](implicit eh: Lazy[Empty[H]], et: Lazy[MkEmpty[T]])
implicit def mkEmptyHcons[H, T <: HList](implicit eh: Lazy[Empty[H]], et: Lazy[MkEmpty[T]])
: MkEmpty[H :: T] = new MkEmpty[H :: T] {
val empty = eh.value.empty :: et.value.empty
}

implicit def generic[T, R](implicit gen: Generic.Aux[T, R], er: Lazy[MkEmpty[R]])
implicit def mkEmptyGeneric[T, R](implicit gen: Generic.Aux[T, R], er: Lazy[MkEmpty[R]])
: MkEmpty[T] = new MkEmpty[T] {
val empty = gen.from(er.value.empty)
}
Expand Down
23 changes: 11 additions & 12 deletions core/src/main/scala/cats/derived/emptyk.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,31 @@
package cats.derived

import alleycats.{ EmptyK, Pure }
import export.{ exports, reexports }
import shapeless._

@reexports[MkEmptyK]
object emptyk

trait MkEmptyK[F[_]] extends EmptyK[F]

@exports
object MkEmptyK extends MkEmptyK0 {
object MkEmptyK extends MkEmptyKDerivation {
def apply[F[_]](implicit mef: MkEmptyK[F]): MkEmptyK[F] = mef
}

trait MkEmptyKDerivation extends MkEmptyK0 {

implicit val hnil: MkEmptyK[Const[HNil]#λ] =
implicit val mkEmptyKHnil: MkEmptyK[Const[HNil]#λ] =
new MkEmptyK[Const[HNil]#λ] {
def empty[A]: HNil = HNil
}

implicit def hcons[F[_]](implicit ihf: IsHCons1[F, EmptyK, MkEmptyK]): MkEmptyK[F] =
implicit def mkEmptyKHcons[F[_]](implicit ihf: IsHCons1[F, EmptyK, MkEmptyK]): MkEmptyK[F] =
new MkEmptyK[F] {
def empty[A]: F[A] = {
import ihf._
pack((fh.empty, ft.empty))
}
}

implicit def ccons0[F[_]](implicit icf: IsCCons1[F, EmptyK, Trivial1]): MkEmptyK[F] =
implicit def mkEmptyKCcons0[F[_]](implicit icf: IsCCons1[F, EmptyK, Trivial1]): MkEmptyK[F] =
new MkEmptyK[F] {
def empty[A]: F[A] = {
import icf._
Expand All @@ -52,7 +51,7 @@ object MkEmptyK extends MkEmptyK0 {
}

trait MkEmptyK0 extends MkEmptyK1 {
implicit def ccons1[F[_]](implicit icf: IsCCons1[F, Trivial1, MkEmptyK]): MkEmptyK[F] =
implicit def mkEmptyKCcons1[F[_]](implicit icf: IsCCons1[F, Trivial1, MkEmptyK]): MkEmptyK[F] =
new MkEmptyK[F] {
def empty[A]: F[A] = {
import icf._
Expand All @@ -62,7 +61,7 @@ trait MkEmptyK0 extends MkEmptyK1 {
}

trait MkEmptyK1 extends MkEmptyK2 {
implicit def split0[F[_]](implicit split: Split1[F, EmptyK, Trivial1]): MkEmptyK[F] =
implicit def mkEmptyKSplit0[F[_]](implicit split: Split1[F, EmptyK, Trivial1]): MkEmptyK[F] =
new MkEmptyK[F] {
def empty[A]: F[A] = {
import split._
Expand All @@ -72,7 +71,7 @@ trait MkEmptyK1 extends MkEmptyK2 {
}

trait MkEmptyK2 extends MkEmptyK3 {
implicit def split1[F[_]](implicit split: Split1[F, Pure, EmptyK]): MkEmptyK[F] =
implicit def mkEmptyKSplit1[F[_]](implicit split: Split1[F, Pure, EmptyK]): MkEmptyK[F] =
new MkEmptyK[F] {
def empty[A]: F[A] = {
import split._
Expand All @@ -82,7 +81,7 @@ trait MkEmptyK2 extends MkEmptyK3 {
}

trait MkEmptyK3 {
implicit def generic[F[_]](implicit gen: Generic1[F, MkEmptyK]): MkEmptyK[F] =
implicit def mkEmptyKGeneric[F[_]](implicit gen: Generic1[F, MkEmptyK]): MkEmptyK[F] =
new MkEmptyK[F] {
def empty[A]: F[A] = gen.from(gen.fr.empty)
}
Expand Down
21 changes: 8 additions & 13 deletions core/src/main/scala/cats/derived/eq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,32 @@
package cats.derived

import cats.Eq
import export.{ exports, imports, reexports }
import shapeless._

@reexports[MkEq]
object eq {
@imports[Eq]
object legacy
}

trait MkEq[T] extends Eq[T]

@exports
object MkEq {
object MkEq extends MkEqDerivation {
def apply[T](implicit met: MkEq[T]): MkEq[T] = met
}

implicit val hnil: MkEq[HNil] =
trait MkEqDerivation {
implicit val mkEqHnil: MkEq[HNil] =
new MkEq[HNil] {
def eqv(a: HNil, b: HNil) = true
}

implicit def hcons[H, T <: HList](implicit eqH: Lazy[Eq[H]], eqT: Lazy[MkEq[T]]): MkEq[H :: T] =
implicit def mkEqHcons[H, T <: HList](implicit eqH: Lazy[Eq[H]], eqT: Lazy[MkEq[T]]): MkEq[H :: T] =
new MkEq[H :: T] {
def eqv(a: H :: T, b: H :: T) = eqH.value.eqv(a.head, b.head) && eqT.value.eqv(a.tail, b.tail)
}

implicit val cnil: MkEq[CNil] =
implicit val mkEqCnil: MkEq[CNil] =
new MkEq[CNil] {
def eqv(a: CNil, b: CNil) = true
}

implicit def ccons[L, R <: Coproduct](implicit eqL: Lazy[Eq[L]], eqR: Lazy[MkEq[R]]): MkEq[L :+: R] =
implicit def mkEqCcons[L, R <: Coproduct](implicit eqL: Lazy[Eq[L]], eqR: Lazy[MkEq[R]]): MkEq[L :+: R] =
new MkEq[L :+: R] {
def eqv(a: L :+: R, b: L :+: R) = (a, b) match {
case (Inl(l1), Inl(l2)) => eqL.value.eqv(l1, l2)
Expand All @@ -56,7 +51,7 @@ object MkEq {
}
}

implicit def generic[T, R](implicit gen: Generic.Aux[T, R], eqR: Lazy[MkEq[R]]): MkEq[T] =
implicit def mkEqGeneric[T, R](implicit gen: Generic.Aux[T, R], eqR: Lazy[MkEq[R]]): MkEq[T] =
new MkEq[T] {
def eqv(a: T, b: T) = eqR.value.eqv(gen.to(a), gen.to(b))
}
Expand Down
Loading

0 comments on commit d898a33

Please sign in to comment.