Skip to content

Commit

Permalink
addressed misc feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
kailuowang committed Apr 17, 2017
1 parent 1fb29b0 commit d8bc6e1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
24 changes: 19 additions & 5 deletions core/src/main/scala/cats/data/Const.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import cats.functor.Contravariant
* [[Const]] can be seen as a type level version of `Function.const[A, B]: A => B => A`
* B is set covariant to help type inference.
*/
final case class Const[A, +B](getConst: A) {
final case class Const[A, B](getConst: A) {
/**
* changes the type of the second type parameter
*/
def retag[C]: Const[A, C] =
this.asInstanceOf[Const[A, C]]

def combine[BB >: B](that: Const[A, BB])(implicit A: Semigroup[A]): Const[A, BB] =
def combine(that: Const[A, B])(implicit A: Semigroup[A]): Const[A, B] =
Const(A.combine(getConst, that.getConst))

def traverseFilter[F[_], C](f: B => F[Option[C]])(implicit F: Applicative[F]): F[Const[A, C]] =
Expand All @@ -24,13 +24,13 @@ final case class Const[A, +B](getConst: A) {
def traverse[F[_], C](f: B => F[C])(implicit F: Applicative[F]): F[Const[A, C]] =
F.pure(retag[C])

def ===[BB >: B](that: Const[A, BB])(implicit A: Eq[A]): Boolean =
def ===(that: Const[A, B])(implicit A: Eq[A]): Boolean =
A.eqv(getConst, that.getConst)

def partialCompare[BB >: B](that: Const[A, BB])(implicit A: PartialOrder[A]): Double =
def partialCompare(that: Const[A, B])(implicit A: PartialOrder[A]): Double =
A.partialCompare(getConst, that.getConst)

def compare[BB >: B](that: Const[A, BB])(implicit A: Order[A]): Int =
def compare(that: Const[A, B])(implicit A: Order[A]): Int =
A.compare(getConst, that.getConst)

def show(implicit A: Show[A]): String =
Expand All @@ -40,6 +40,20 @@ final case class Const[A, +B](getConst: A) {
object Const extends ConstInstances {
def empty[A, B](implicit A: Monoid[A]): Const[A, B] =
Const(A.empty)

final class OfPartiallyApplied[B] {
def apply[A](a: A): Const[A, B] = Const(a)
}

/**
* Convenient syntax for creating a Const[A, B] from an `A`
* {{{
* scala> import cats.data._
* scala> Const.of[Int]("a")
* res0: Const[String, Int] = Const("a")
* }}}
*/
def of[B]: OfPartiallyApplied[B] = new OfPartiallyApplied
}

private[data] sealed abstract class ConstInstances extends ConstInstances0 {
Expand Down
5 changes: 5 additions & 0 deletions free/src/test/scala/cats/free/FreeTTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ class FreeTTests extends CatsSuite {
result.toString.length should be > 0
}

private[free] def liftTCompilationTests() = {
val a: Either[String, Int]= Right(42)
val b: FreeT[Option, Either[String, ?], Int] = FreeT.liftT(a)
}

}

object FreeTTests extends FreeTTestsInstances {
Expand Down
2 changes: 1 addition & 1 deletion tests/src/test/scala/cats/tests/RegressionTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class RegressionTests extends CatsSuite {

test("#500: foldMap - traverse consistency") {
assert(
List(1,2,3).traverse(i => Const(List(i))).getConst == List(1,2,3).foldMap(List(_))
List(1,2,3).traverse(i => Const.of[List[Int]](List(i))).getConst == List(1,2,3).foldMap(List(_))
)
}

Expand Down

0 comments on commit d8bc6e1

Please sign in to comment.