Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into s-gosub-flatmap
Browse files Browse the repository at this point in the history
  • Loading branch information
adelbertc committed Jun 18, 2016
2 parents 40c5fab + 86f6a5e commit 49adec6
Show file tree
Hide file tree
Showing 113 changed files with 1,183 additions and 416 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ The current maintainers (people who can merge pull requests) are:
* [milessabin](https://github.com/milessabin) Miles Sabin
* [fthomas](https://github.com/fthomas) Frank Thomas
* [julien-truffaut](https://github.com/julien-truffaut) Julien Truffaut
* [kailuowang](https://github.com/kailuowang) Kailuo Wang

We are currently following a practice of requiring at least two
sign-offs to merge PRs (and for large or contentious issues we may
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ lazy val testingDependencies = Seq(
def docsSourcesAndProjects(sv: String): (Boolean, Seq[ProjectReference]) =
CrossVersion.partialVersion(sv) match {
case Some((2, 10)) => (false, Nil)
case _ => (true, Seq(coreJVM, freeJVM))
case _ => (true, Seq(kernelJVM, coreJVM, freeJVM))
}

lazy val javadocSettings = Seq(
Expand Down
9 changes: 9 additions & 0 deletions core/src/main/scala/cats/Applicative.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,12 @@ import simulacrum.typeclass
val G = Applicative[G]
}
}

object Applicative {
def monoid[F[_], A](implicit f: Applicative[F], monoid: Monoid[A]): Monoid[F[A]] =
new ApplicativeMonoid[F, A](f, monoid)
}

private[cats] class ApplicativeMonoid[F[_], A](f: Applicative[F], monoid: Monoid[A]) extends ApplySemigroup(f, monoid) with Monoid[F[A]] {
def empty: F[A] = f.pure(monoid.empty)
}
10 changes: 10 additions & 0 deletions core/src/main/scala/cats/Apply.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,13 @@ trait Apply[F[_]] extends Functor[F] with Cartesian[F] with ApplyArityFunctions[
val G = Apply[G]
}
}

object Apply {
def semigroup[F[_], A](implicit f: Apply[F], sg: Semigroup[A]): Semigroup[F[A]] =
new ApplySemigroup[F, A](f, sg)
}

private[cats] class ApplySemigroup[F[_], A](f: Apply[F], sg: Semigroup[A]) extends Semigroup[F[A]] {
def combine(a: F[A], b: F[A]): F[A] =
f.map2(a, b)(sg.combine)
}
8 changes: 4 additions & 4 deletions core/src/main/scala/cats/Bifoldable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ trait Bifoldable[F[_, _]] extends Any with Serializable { self =>
)

def compose[G[_, _]](implicit ev: Bifoldable[G]): Bifoldable[λ[(α, β) => F[G[α, β], G[α, β]]]] =
new CompositeBifoldable[F, G] {
new ComposedBifoldable[F, G] {
val F = self
val G = ev
}
Expand All @@ -28,17 +28,17 @@ object Bifoldable {
def apply[F[_, _]](implicit F: Bifoldable[F]): Bifoldable[F] = F
}

trait CompositeBifoldable[F[_, _], G[_, _]] extends Bifoldable[λ[(α, β) => F[G[α, β], G[α, β]]]] {
private[cats] trait ComposedBifoldable[F[_, _], G[_, _]] extends Bifoldable[λ[(α, β) => F[G[α, β], G[α, β]]]] {
implicit def F: Bifoldable[F]
implicit def G: Bifoldable[G]

def bifoldLeft[A, B, C](fab: F[G[A, B], G[A, B]], c: C)(f: (C, A) => C, g: (C, B) => C): C =
override def bifoldLeft[A, B, C](fab: F[G[A, B], G[A, B]], c: C)(f: (C, A) => C, g: (C, B) => C): C =
F.bifoldLeft(fab, c)(
(c: C, gab: G[A, B]) => G.bifoldLeft(gab, c)(f, g),
(c: C, gab: G[A, B]) => G.bifoldLeft(gab, c)(f, g)
)

def bifoldRight[A, B, C](fab: F[G[A, B], G[A, B]], c: Eval[C])(f: (A, Eval[C]) => Eval[C], g: (B, Eval[C]) => Eval[C]): Eval[C] =
override def bifoldRight[A, B, C](fab: F[G[A, B], G[A, B]], c: Eval[C])(f: (A, Eval[C]) => Eval[C], g: (B, Eval[C]) => Eval[C]): Eval[C] =
F.bifoldRight(fab, c)(
(gab: G[A, B], c: Eval[C]) => G.bifoldRight(gab, c)(f, g),
(gab: G[A, B], c: Eval[C]) => G.bifoldRight(gab, c)(f, g)
Expand Down
11 changes: 6 additions & 5 deletions core/src/main/scala/cats/Bitraverse.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cats

import cats.functor.Bifunctor
import cats.functor.{Bifunctor, ComposedBifunctor}

/**
* A type class abstracting over types that give rise to two independent [[cats.Traverse]]s.
Expand All @@ -15,7 +15,7 @@ trait Bitraverse[F[_, _]] extends Bifoldable[F] with Bifunctor[F] { self =>

/** If F and G are both [[cats.Bitraverse]] then so is their composition F[G[_, _], G[_, _]] */
def compose[G[_, _]](implicit ev: Bitraverse[G]): Bitraverse[λ[(α, β) => F[G[α, β], G[α, β]]]] =
new CompositeBitraverse[F, G] {
new ComposedBitraverse[F, G] {
val F = self
val G = ev
}
Expand All @@ -28,13 +28,14 @@ object Bitraverse {
def apply[F[_, _]](implicit F: Bitraverse[F]): Bitraverse[F] = F
}

trait CompositeBitraverse[F[_, _], G[_, _]]
private[cats] trait ComposedBitraverse[F[_, _], G[_, _]]
extends Bitraverse[λ[(α, β) => F[G[α, β], G[α, β]]]]
with CompositeBifoldable[F, G] {
with ComposedBifoldable[F, G]
with ComposedBifunctor[F,G] {
def F: Bitraverse[F]
def G: Bitraverse[G]

def bitraverse[H[_]: Applicative, A, B, C, D](
override def bitraverse[H[_]: Applicative, A, B, C, D](
fab: F[G[A, B], G[A, B]])(
f: A => H[C], g: B => H[D]
): H[F[G[C, D], G[C, D]]] =
Expand Down
14 changes: 7 additions & 7 deletions core/src/main/scala/cats/Eval.scala
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ object Eval extends EvalInstances {

private[cats] trait EvalInstances extends EvalInstances0 {

implicit val evalBimonad: Bimonad[Eval] with MonadRec[Eval] =
implicit val catsBimonadForEval: Bimonad[Eval] with MonadRec[Eval] =
new Bimonad[Eval] with MonadRec[Eval] {
override def map[A, B](fa: Eval[A])(f: A => B): Eval[B] = fa.map(f)
def pure[A](a: A): Eval[A] = Now(a)
Expand All @@ -310,35 +310,35 @@ private[cats] trait EvalInstances extends EvalInstances0 {
})
}

implicit def evalOrder[A: Order]: Order[Eval[A]] =
implicit def catsOrderForEval[A: Order]: Order[Eval[A]] =
new Order[Eval[A]] {
def compare(lx: Eval[A], ly: Eval[A]): Int =
lx.value compare ly.value
}

implicit def evalGroup[A: Group]: Group[Eval[A]] =
implicit def catsGroupForEval[A: Group]: Group[Eval[A]] =
new EvalGroup[A] { val algebra: Group[A] = Group[A] }
}

private[cats] trait EvalInstances0 extends EvalInstances1 {
implicit def evalPartialOrder[A: PartialOrder]: PartialOrder[Eval[A]] =
implicit def catsPartialOrderForEval[A: PartialOrder]: PartialOrder[Eval[A]] =
new PartialOrder[Eval[A]] {
def partialCompare(lx: Eval[A], ly: Eval[A]): Double =
lx.value partialCompare ly.value
}

implicit def evalMonoid[A: Monoid]: Monoid[Eval[A]] =
implicit def catsMonoidForEval[A: Monoid]: Monoid[Eval[A]] =
new EvalMonoid[A] { val algebra = Monoid[A] }
}

private[cats] trait EvalInstances1 {
implicit def evalEq[A: Eq]: Eq[Eval[A]] =
implicit def catsEqForEval[A: Eq]: Eq[Eval[A]] =
new Eq[Eval[A]] {
def eqv(lx: Eval[A], ly: Eval[A]): Boolean =
lx.value === ly.value
}

implicit def evalSemigroup[A: Semigroup]: Semigroup[Eval[A]] =
implicit def catsSemigroupForEval[A: Semigroup]: Semigroup[Eval[A]] =
new EvalSemigroup[A] { val algebra = Semigroup[A] }
}

Expand Down
11 changes: 11 additions & 0 deletions core/src/main/scala/cats/Foldable.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cats

import scala.collection.mutable
import cats.std.long._
import simulacrum.typeclass

/**
Expand Down Expand Up @@ -56,6 +57,16 @@ import simulacrum.typeclass
}
}

/**
* The size of this Foldable.
*
* This is overriden in structures that have more efficient size implementations
* (e.g. Vector, Set, Map).
*
* Note: will not terminate for infinite-sized collections.
*/
def size[A](fa: F[A]): Long = foldMap(fa)(_ => 1)

/**
* Fold implemented using the given Monoid[A] instance.
*/
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/cats/NotNull.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ object NotNull {

implicit def `If you are seeing this, you probably need to add an explicit type parameter somewhere, because Null is being inferred.`: NotNull[Null] = throw ambiguousException

implicit def ambiguousNull2: NotNull[Null] = throw ambiguousException
implicit def catsAmbiguousNotNullNull2: NotNull[Null] = throw ambiguousException

implicit def notNull[A]: NotNull[A] = singleton.asInstanceOf[NotNull[A]]
implicit def catsNotNullForA[A]: NotNull[A] = singleton.asInstanceOf[NotNull[A]]
}
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/Show.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ object Show {
def show(a: A): String = a.toString
}

implicit val showContravariant: Contravariant[Show] = new Contravariant[Show] {
implicit val catsContravariantForShow: Contravariant[Show] = new Contravariant[Show] {
def contramap[A, B](fa: Show[A])(f: B => A): Show[B] =
show[B](fa.show _ compose f)
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/Trivial.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ object Trivial {
type P3[A, B, C] = Trivial
type P3H1[F[_], A, B, C] = Trivial

implicit val manifest: Trivial = new Trivial {}
implicit val catsTrivialInstance: Trivial = new Trivial {}
}
20 changes: 10 additions & 10 deletions core/src/main/scala/cats/Unapply.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ object Unapply extends Unapply2Instances {
type A = AA
}

implicit def unapply1[TC[_[_]], F[_], AA](implicit tc: TC[F])
implicit def catsUnapply1[TC[_[_]], F[_], AA](implicit tc: TC[F])
: Aux1[TC,F[AA],F,AA] =
new Unapply[TC,F[AA]] {
type M[X] = F[X]
Expand Down Expand Up @@ -80,29 +80,29 @@ private[cats] sealed abstract class Unapply2Instances extends Unapply3Instances
}


implicit def unapply2left[TC[_[_]], F[_,_], AA, B](implicit tc: TC[F[?,B]]): Aux2Left[TC,F[AA,B], F, AA, B] = new Unapply[TC, F[AA,B]] {
implicit def catsUnapply2left[TC[_[_]], F[_,_], AA, B](implicit tc: TC[F[?,B]]): Aux2Left[TC,F[AA,B], F, AA, B] = new Unapply[TC, F[AA,B]] {
type M[X] = F[X, B]
type A = AA
def TC: TC[F[?, B]] = tc
def subst: F[AA, B] => M[A] = identity
}

implicit def unapply2right[TC[_[_]], F[_,_], AA, B](implicit tc: TC[F[AA,?]]): Aux2Right[TC,F[AA,B], F, AA, B] = new Unapply[TC, F[AA,B]] {
implicit def catsUnapply2right[TC[_[_]], F[_,_], AA, B](implicit tc: TC[F[AA,?]]): Aux2Right[TC,F[AA,B], F, AA, B] = new Unapply[TC, F[AA,B]] {
type M[X] = F[AA, X]
type A = B
def TC: TC[F[AA, ?]] = tc
def subst: F[AA, B] => M[A] = identity
}


implicit def unapply2leftK[TC[_[_]], F[_,_[_]], AA, B[_]](implicit tc: TC[F[?,B]]): Aux2LeftK[TC,F[AA,B], F, AA, B] = new Unapply[TC, F[AA,B]] {
implicit def catsUnapply2leftK[TC[_[_]], F[_,_[_]], AA, B[_]](implicit tc: TC[F[?,B]]): Aux2LeftK[TC,F[AA,B], F, AA, B] = new Unapply[TC, F[AA,B]] {
type M[X] = F[X, B]
type A = AA
def TC: TC[F[?, B]] = tc
def subst: F[AA, B] => M[A] = identity
}

implicit def unapply2rightK[TC[_[_]], F[_[_],_], AA[_], B](implicit tc: TC[F[AA,?]]): Aux2RightK[TC,F[AA,B], F, AA, B] = new Unapply[TC, F[AA,B]] {
implicit def catsUnapply2rightK[TC[_[_]], F[_[_],_], AA[_], B](implicit tc: TC[F[AA,?]]): Aux2RightK[TC,F[AA,B], F, AA, B] = new Unapply[TC, F[AA,B]] {
type M[X] = F[AA, X]
type A = B
def TC: TC[F[AA, ?]] = tc
Expand All @@ -114,14 +114,14 @@ private[cats] sealed abstract class Unapply2Instances extends Unapply3Instances
// STEW: I'm not sure why these Nothing cases are needed and aren't
// just caught by the generic cases, I'd love for someone to figure
// that out and report back.
implicit def unapply2leftN[TC[_[_]], F[_,+_], AA](implicit tc: TC[F[?,Nothing]]): Aux2Left[TC,F[AA,Nothing], F, AA, Nothing] = new Unapply[TC, F[AA,Nothing]] {
implicit def catsUnapply2leftN[TC[_[_]], F[_,+_], AA](implicit tc: TC[F[?,Nothing]]): Aux2Left[TC,F[AA,Nothing], F, AA, Nothing] = new Unapply[TC, F[AA,Nothing]] {
type M[X] = F[X, Nothing]
type A = AA
def TC: TC[F[?, Nothing]] = tc
def subst: F[AA, Nothing] => M[A] = identity
}

implicit def unapply2rightN[TC[_[_]], F[+_,_], B](implicit tc: TC[F[Nothing,?]]): Aux2Right[TC,F[Nothing,B], F, Nothing, B] = new Unapply[TC, F[Nothing,B]] {
implicit def catsUnapply2rightN[TC[_[_]], F[+_,_], B](implicit tc: TC[F[Nothing,?]]): Aux2Right[TC,F[Nothing,B], F, Nothing, B] = new Unapply[TC, F[Nothing,B]] {
type M[X] = F[Nothing, X]
type A = B
def TC: TC[F[Nothing, ?]] = tc
Expand Down Expand Up @@ -155,14 +155,14 @@ private[cats] sealed abstract class Unapply3Instances {
}


implicit def unapply3MTLeft[TC[_[_]], F[_[_],_,_], AA[_], B, C](implicit tc: TC[F[AA,?,C]]): Aux3MTLeft[TC,F[AA, B, C], F, AA, B, C] = new Unapply[TC, F[AA,B,C]] {
implicit def catsUnapply3MTLeft[TC[_[_]], F[_[_],_,_], AA[_], B, C](implicit tc: TC[F[AA,?,C]]): Aux3MTLeft[TC,F[AA, B, C], F, AA, B, C] = new Unapply[TC, F[AA,B,C]] {
type M[X] = F[AA, X, C]
type A = B
def TC: TC[F[AA, ?, C]] = tc
def subst: F[AA, B, C] => M[A] = identity
}

implicit def unapply3MTright[TC[_[_]], F[_[_],_,_], AA[_], B, C](implicit tc: TC[F[AA,B,?]]): Aux3MTRight[TC,F[AA,B,C], F, AA, B, C] = new Unapply[TC, F[AA,B,C]] {
implicit def catsUnapply3MTright[TC[_[_]], F[_[_],_,_], AA[_], B, C](implicit tc: TC[F[AA,B,?]]): Aux3MTRight[TC,F[AA,B,C], F, AA, B, C] = new Unapply[TC, F[AA,B,C]] {
type M[X] = F[AA, B, X]
type A = C
def TC: TC[F[AA, B, ?]] = tc
Expand All @@ -174,7 +174,7 @@ private[cats] sealed abstract class Unapply3Instances {
type A = C
}

implicit def unapply3Nested[TC[_[_]], MA, F[_[_], _[_], _], AA[_], BB[_], C](implicit tc: TC[F[AA, BB, ?]]): Aux3Nested[TC, F[AA, BB, C], F, AA, BB, C] = new Unapply[TC, F[AA, BB, C]] {
implicit def catsUnapply3Nested[TC[_[_]], MA, F[_[_], _[_], _], AA[_], BB[_], C](implicit tc: TC[F[AA, BB, ?]]): Aux3Nested[TC, F[AA, BB, C], F, AA, BB, C] = new Unapply[TC, F[AA, BB, C]] {
type M[X] = F[AA, BB, X]
type A = C
def TC: TC[F[AA, BB, ?]] = tc
Expand Down
7 changes: 6 additions & 1 deletion core/src/main/scala/cats/data/Cokleisli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cats
package data

import cats.arrow.{Arrow, Split}
import cats.functor.Profunctor
import cats.functor.{Contravariant, Profunctor}
import cats.{CoflatMap, Comonad, Functor, Monad}

/**
Expand Down Expand Up @@ -71,6 +71,11 @@ private[data] sealed abstract class CokleisliInstances0 {

implicit def catsDataSemigroupKForCokleisli[F[_]](implicit ev: CoflatMap[F]): SemigroupK[λ[α => Cokleisli[F, α, α]]] =
new CokleisliSemigroupK[F] { def F: CoflatMap[F] = ev }

implicit def catsDataContravariantForCokleisli[F[_]: Functor, A]: Contravariant[Cokleisli[F, ?, A]] =
new Contravariant[Cokleisli[F, ?, A]] {
def contramap[B, C](fbc: Cokleisli[F, B, A])(f: C => B): Cokleisli[F, C, A] = fbc.lmap(f)
}
}

private trait CokleisliArrow[F[_]] extends Arrow[Cokleisli[F, ?, ?]] with CokleisliSplit[F] with CokleisliProfunctor[F] {
Expand Down
15 changes: 14 additions & 1 deletion core/src/main/scala/cats/data/Func.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cats
package data

import cats.functor.Contravariant

/**
* [[Func]] is a function `A => F[B]`.
*
Expand Down Expand Up @@ -50,6 +52,11 @@ private[data] abstract class FuncInstances1 {
new FuncFunctor[F, C] {
def F: Functor[F] = FF
}

implicit def catsDataContravariantForFunc[F[_], C](implicit FC: Contravariant[F]): Contravariant[λ[α => Func[F, α, C]]] =
new FuncContravariant[F, C] {
def F: Contravariant[F] = FC
}
}

sealed trait FuncFunctor[F[_], C] extends Functor[λ[α => Func[F, C, α]]] {
Expand All @@ -58,6 +65,12 @@ sealed trait FuncFunctor[F[_], C] extends Functor[λ[α => Func[F, C, α]]] {
fa.map(f)(F)
}

sealed trait FuncContravariant[F[_], C] extends Contravariant[λ[α => Func[F, α, C]]] {
def F: Contravariant[F]
def contramap[A, B](fa: Func[F, A, C])(f: B => A): Func[F, B, C] =
Func.func(a => fa.run(f(a)))
}

sealed trait FuncApply[F[_], C] extends Apply[λ[α => Func[F, C, α]]] with FuncFunctor[F, C] {
def F: Apply[F]
def ap[A, B](f: Func[F, C, A => B])(fa: Func[F, C, A]): Func[F, C, B] =
Expand Down Expand Up @@ -88,7 +101,7 @@ sealed abstract class AppFunc[F[_], A, B] extends Func[F, A, B] { self =>
}

def compose[G[_], C](g: AppFunc[G, C, A]): AppFunc[Nested[G, F, ?], C, B] = {
implicit val gfApplicative: Applicative[Nested[G, F, ?]] = Nested.nestedApplicative[G, F](g.F, F)
implicit val gfApplicative: Applicative[Nested[G, F, ?]] = Nested.catsDataApplicativeForNested[G, F](g.F, F)
Func.appFunc[Nested[G, F, ?], C, B]({
c: C => Nested(g.F.map(g.run(c))(self.run))
})
Expand Down
Loading

0 comments on commit 49adec6

Please sign in to comment.