Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into std-to-instances
Browse files Browse the repository at this point in the history
  • Loading branch information
ceedubs committed Jun 22, 2016
2 parents e2590c6 + fffc18a commit e0e098e
Show file tree
Hide file tree
Showing 20 changed files with 79 additions and 102 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ This release includes some API changes:

And additions:

* [#853](https://github.com/typelevel/cats/pull/853): Adds a new `LiftTrans` typeclass
* [#853](https://github.com/typelevel/cats/pull/853): Adds a new `LiftTrans` type class
* [#864](https://github.com/typelevel/cats/pull/864): Add `Bifoldable`
* [#875](https://github.com/typelevel/cats/pull/875): Add `.get` method to `StateT`
* [#884](https://github.com/typelevel/cats/pull/884): Add `Applicative` syntax
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ Cats will be designed to use modern *best practices*:
* [scalacheck](http://scalacheck.org) for property-based testing
* [discipline](https://github.com/typelevel/discipline) for encoding and testing laws
* [kind-projector](https://github.com/non/kind-projector) for type lambda syntax
* [algebra](https://github.com/non/algebra) for shared algebraic structures
* ...and of course a pure functional subset of the Scala language.

(We also plan to support [Miniboxing](http://scala-miniboxing.org) in a branch.)
Expand Down
18 changes: 17 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,30 @@ import com.typesafe.sbt.SbtGhPages.GhPagesKeys._
import sbtunidoc.Plugin.UnidocKeys._
import ReleaseTransformations._
import ScoverageSbtPlugin._
import scala.xml.transform.{RewriteRule, RuleTransformer}

lazy val botBuild = settingKey[Boolean]("Build by TravisCI instead of local dev environment")

lazy val scoverageSettings = Seq(
ScoverageKeys.coverageMinimum := 60,
ScoverageKeys.coverageFailOnMinimum := false,
ScoverageKeys.coverageHighlighting := scalaBinaryVersion.value != "2.10",
ScoverageKeys.coverageExcludedPackages := "cats\\.bench\\..*"
ScoverageKeys.coverageExcludedPackages := "cats\\.bench\\..*",
// don't include scoverage as a dependency in the pom
// see issue #980
// this code was copied from https://github.com/mongodb/mongo-spark
pomPostProcess := { (node: xml.Node) =>
new RuleTransformer(
new RewriteRule {
override def transform(node: xml.Node): Seq[xml.Node] = node match {
case e: xml.Elem
if e.label == "dependency" && e.child.exists(child => child.label == "groupId" && child.text == "org.scoverage") => Nil
case _ => Seq(node)

}

}).transform(node).head
}
)

lazy val buildSettings = Seq(
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/cats/TransLift.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package cats


/**
* A typeclass which abstracts over the ability to lift an M[A] into a
* A type class which abstracts over the ability to lift an M[A] into a
* MonadTransformer
*/
trait TransLift[MT[_[_], _]] {

/**
* The typeclass which constrains liftT as a function of the type
* The type class which constrains liftT as a function of the type
* constructor it is given. A safe "identity" value for this type
* if your transformer does not constrain its lifted effects would
* be `type TC[M[_]] = Trivial`. A more common constraint might be
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/cats/Trivial.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package cats

/**
* The "Unit typeclass". The only instance of `Trivial` is given by
* The "Unit type class". The only instance of `Trivial` is given by
* `Trivial.manifest`, and this instance is guaranteed to be in the
* implicit scope. Several convenience type aliases are provided in
* companion object, covering a few common use cases and avoiding the
* need for unnecessary lambdas (e.g. if you want a trivial typeclass
* need for unnecessary lambdas (e.g. if you want a trivial type class
* instance for a type constructor, you should use `Trivial.PH1`).
*/
sealed trait Trivial
Expand Down
12 changes: 10 additions & 2 deletions core/src/main/scala/cats/instances/try.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import TryInstances.castFailure

import scala.util.control.NonFatal
import scala.util.{Failure, Success, Try}
import scala.annotation.tailrec

trait TryInstances extends TryInstances1 {

// scalastyle:off method.length
implicit def catsStdInstancesForTry: MonadError[Try, Throwable] with CoflatMap[Try] with Traverse[Try] =
new TryCoflatMap with MonadError[Try, Throwable] with Traverse[Try] {
implicit def catsStdInstancesForTry: MonadError[Try, Throwable] with CoflatMap[Try] with Traverse[Try] with MonadRec[Try] =
new TryCoflatMap with MonadError[Try, Throwable] with Traverse[Try] with MonadRec[Try] {
def pure[A](x: A): Try[A] = Success(x)

override def pureEval[A](x: Eval[A]): Try[A] = x match {
Expand Down Expand Up @@ -57,6 +58,13 @@ trait TryInstances extends TryInstances1 {
case f: Failure[_] => G.pure(castFailure[B](f))
}

@tailrec final def tailRecM[B, C](b: B)(f: B => Try[(B Xor C)]): Try[C] =
f(b) match {
case f: Failure[_] => castFailure[C](f)
case Success(Xor.Left(b1)) => tailRecM(b1)(f)
case Success(Xor.Right(c)) => Success(c)
}

def handleErrorWith[A](ta: Try[A])(f: Throwable => Try[A]): Try[A] =
ta.recoverWith { case t => f(t) }

Expand Down
4 changes: 2 additions & 2 deletions docs/src/main/tut/contravariant.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ but with the `f` transformation reversed.
Generally speaking, if you have some context `F[A]` for type `A`,
and you can get an `A` value out of a `B` value — `Contravariant` allows you to get the `F[B]` context for `B`.

Examples of `Contravariant` instances are [`Show`](show.html) and `scala.math.Ordering` (along with `algebra.Order`).
Examples of `Contravariant` instances are [`Show`](show.html) and `scala.math.Ordering` (along with `cats.kernel.Order`).

## Contravariant instance for Show.

Expand Down Expand Up @@ -51,7 +51,7 @@ Salary(Money(1000)).show

`Show` example is trivial and quite far-fetched, let's see how `Contravariant` can help with orderings.

`scala.math.Ordering` typeclass defines comparison operations, e.g. `compare`:
`scala.math.Ordering` type class defines comparison operations, e.g. `compare`:

```tut:book
Ordering.Int.compare(2, 1)
Expand Down
2 changes: 1 addition & 1 deletion docs/src/main/tut/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import cats.data._
import cats.implicits._
```

The `cats._` import brings in quite a few [typeclasses](http://typelevel.org/cats/typeclasses.html) (similar to interfaces) such as [Monad](http://typelevel.org/cats/tut/monad.html), [Semigroup](http://typelevel.org/cats/tut/semigroup.html), and [Foldable](http://typelevel.org/cats/tut/foldable.html). Instead of the entire `cats` package, you can import only the types that you need, for example:
The `cats._` import brings in quite a few [type classes](http://typelevel.org/cats/typeclasses.html) (similar to interfaces) such as [Monad](http://typelevel.org/cats/tut/monad.html), [Semigroup](http://typelevel.org/cats/tut/semigroup.html), and [Foldable](http://typelevel.org/cats/tut/foldable.html). Instead of the entire `cats` package, you can import only the types that you need, for example:

```tut:silent
import cats.Monad
Expand Down
12 changes: 4 additions & 8 deletions docs/src/main/tut/freemonad.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,24 +477,20 @@ If you look at implementation in cats, you will see another member of
the `Free[_]` ADT:

```scala
sealed abstract case class Gosub[S[_], B]() extends Free[S, B] {
type C
val a: () => Free[S, C]
val f: C => Free[S, B]
}
case class FlatMapped[S[_], B, C](c: Free[S, C], f: C => Free[S, B]) extends Free[S, B]
```

`Gosub` represents a call to a subroutine `a` and when `a` is
`FlatMapped` represents a call to a subroutine `c` and when `c` is
finished, it continues the computation by calling the function `f`
with the result of `a`.
with the result of `c`.

It is actually an optimization of `Free` structure allowing to solve a
problem of quadratic complexity implied by very deep recursive `Free`
computations.

It is exactly the same problem as repeatedly appending to a `List[_]`.
As the sequence of operations becomes longer, the slower a `flatMap`
"through" the structure will be. With `Gosub`, `Free` becomes a
"through" the structure will be. With `FlatMapped`, `Free` becomes a
right-associated structure not subject to quadratic complexity.

## Future Work (TODO)
Expand Down
2 changes: 1 addition & 1 deletion docs/src/main/tut/invariant.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ timestamp. Let's say that we want to create a `Semigroup[Date]`, by
### Semigroup does not form a covariant functor

If `Semigroup` had an instance for the standard covariant [`Functor`](functor.html)
typeclass, we could use `map` to apply a function `longToDate`:
type class, we could use `map` to apply a function `longToDate`:

```tut:silent
import java.util.Date
Expand Down
15 changes: 6 additions & 9 deletions docs/src/main/tut/monoid.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
layout: default
title: "Monoid"
section: "typeclasses"
source: "https://github.com/non/algebra/blob/master/core/src/main/scala/algebra/Monoid.scala"

source: "kernel/src/main/scala/cats/kernel/Monoid.scala"
scaladoc: "#cats.kernel.Monoid"
---
# Monoid

Expand Down Expand Up @@ -70,10 +70,7 @@ l.foldMap(i => (i, i.toString)) // do both of the above in one pass, hurrah!
-------------------------------------------------------------------------------

N.B.
Cats does not define a `Monoid` type class itself, it uses the [`Monoid`
trait](https://github.com/non/algebra/blob/master/core/src/main/scala/algebra/Monoid.scala)
which is defined in the [algebra project](https://github.com/non/algebra) on
which it depends. The [`cats` package object](https://github.com/typelevel/cats/blob/master/core/src/main/scala/cats/package.scala)
defines type aliases to the `Monoid` from algebra, so that you can
`import cats.Monoid`. Also the `Monoid` instance for tuple is also [implemented in algebra](https://github.com/non/algebra/blob/v0.4.2/project/Boilerplate.scala#L80-L217),
cats merely provides it through [inheritance](https://github.com/typelevel/cats/blob/v0.5.0/core/src/main/scala/cats/std/tuple.scala).
Cats defines the `Monoid` type class in cats-kernel. The [`cats` package object](https://github.com/typelevel/cats/blob/master/core/src/main/scala/cats/package.scala)
defines type aliases to the `Monoid` from cats-kernel, so that you can
`import cats.Monoid`. Also the `Monoid` instance for tuple is also [implemented in cats-kernel](https://github.com/typelevel/cats/blob/master/project/KernelBoiler.scala),
cats merely provides it through [inheritance](https://github.com/typelevel/cats/blob/master/core/src/main/scala/cats/std/tuple.scala).
11 changes: 4 additions & 7 deletions docs/src/main/tut/semigroup.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
layout: default
title: "Semigroup"
section: "typeclasses"
source: "https://github.com/non/algebra/blob/master/core/src/main/scala/algebra/Semigroup.scala"

source: "kernel/src/main/scala/cats/kernel/Semigroup.scala"
scaladoc: "#cats.kernel.Semigroup"
---
# Semigroup

Expand Down Expand Up @@ -93,9 +93,6 @@ None |+| Some(1)
```

N.B.
Cats does not define a `Semigroup` type class itself, it uses the [`Semigroup`
trait](https://github.com/non/algebra/blob/master/core/src/main/scala/algebra/Semigroup.scala)
which is defined in the [algebra project](https://github.com/non/algebra) on
which it depends. The [`cats` package object](https://github.com/typelevel/cats/blob/master/core/src/main/scala/cats/package.scala)
defines type aliases to the `Semigroup` from algebra, so that you can
Cats defines the `Semigroup` type class in cats-kernel. The [`cats` package object](https://github.com/typelevel/cats/blob/master/core/src/main/scala/cats/package.scala)
defines type aliases to the `Semigroup` from cats-kernel, so that you can
`import cats.Semigroup`.
54 changes: 8 additions & 46 deletions docs/src/main/tut/semigroupk.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,7 @@ scaladoc: "#cats.SemigroupK"
---
# SemigroupK

Before introducing a `SemigroupK`, it makes sense to talk about what a
`Semigroup` is. A semigroup for some given type `A` has a single operation
(which we will call `combine`), which takes two values of type `A`, and
returns a value of type `A`. This operation must be guaranteed to be
associative. That is to say that:

```scala
((a combine b) combine c)
```

must be the same as

```scala
(a combine (b combine c))
```

for all possible values of `a`, `b`, `c`.

Cats does not define a `Semigroup` type class itself. Instead, we use the
[`Semigroup`
trait](https://github.com/non/algebra/blob/master/core/src/main/scala/algebra/Semigroup.scala)
which is defined in the [algebra
project](https://github.com/non/algebra). The [`cats` package
object](https://github.com/typelevel/cats/blob/master/core/src/main/scala/cats/package.scala)
defines type aliases to the `Semigroup` from algebra, so that you can
`import cats.semigroup`.

There are instances of `Semigroup` defined for many types found in the
scala common library:

```tut:silent
import cats._
import cats.implicits._
```

Examples.

```tut:book
Semigroup[Int].combine(1, 2)
Semigroup[List[Int]].combine(List(1,2,3), List(4,5,6))
Semigroup[Option[Int]].combine(Option(1), Option(2))
Semigroup[Option[Int]].combine(Option(1), None)
Semigroup[Int => Int].combine({(x: Int) => x + 1},{(x: Int) => x * 10}).apply(6)
```

`SemigroupK` has a very similar structure to `Semigroup`, the difference
`SemigroupK` has a very similar structure to [`Semigroup`](semigroup.html), the difference
is that `SemigroupK` operates on type constructors of one argument. So, for
example, whereas you can find a `Semigroup` for types which are fully
specified like `Int` or `List[Int]` or `Option[Int]`, you will find
Expand All @@ -64,6 +19,13 @@ takes a concrete type, like `Int`, and returns a concrete type:
*`, whereas `Int` would have kind `*` and `Map` would have kind `*,* -> *`,
and, in fact, the `K` in `SemigroupK` stands for `Kind`.

First some imports.

```tut:silent
import cats._
import cats.implicits._
```

For `List`, the `Semigroup` instance's `combine` operation and the `SemigroupK`
instance's `combineK` operation are both list concatenation:

Expand Down
4 changes: 2 additions & 2 deletions docs/src/main/tut/symbols.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ All other symbols can be imported with `import cats.implicits._`

A scaladoc generated list is also available on the [Scaladoc symbols page](http://typelevel.org/cats/api/#index.index-_).

| Symbol | Name | Typeclass | Definition |
| Symbol | Name | Type Class | Definition |
| ---------- | ---------------------- | ---------------------------------------------------------------------------------------- |--------------------------------------- |
| `fa |@| fb`| Cartesian builder | [`Cartesian[F[_]]`]({{ site.sources }}/core/src/main/scala/cats/Cartesian.scala) | `|@|(fa: F[A])(fb: F[B]): F[(A, B)]` |
| `fa *> fb` | right apply | [`Cartesian[F[_]]`]({{ site.sources }}/core/src/main/scala/cats/Cartesian.scala) | `*>(fa: F[A])(fb: F[B]): F[A]` |
Expand All @@ -28,4 +28,4 @@ A scaladoc generated list is also available on the [Scaladoc symbols page](http:
| `F :<: G` | inject | [`Inject[F[_], G[_]]`]({{ site.sources }}/free/src/main/scala/cats/free/package.scala) | `Inject` alias |
| `F :≺: G` | inject | [`Inject[F[_], G[_]]`]({{ site.sources }}/free/src/main/scala/cats/free/package.scala) | `Inject` alias |
| `` | bottom | [N/A]({{ site.sources }}/core/src/main/scala/cats/package.scala) | `Nothing` |
| `` | top | [N/A]({{ site.sources }}/core/src/main/scala/cats/package.scala) | `Any` |
| `` | top | [N/A]({{ site.sources }}/core/src/main/scala/cats/package.scala) | `Any` |
2 changes: 1 addition & 1 deletion docs/src/site/_layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ <h1>Cats</h1>
<a href="{{ site.baseurl }}/index.html">About</a>
</li>
<li {%if page.section =="typeclasses" %} class="active" {% else %} class="inactive" {% endif%}>
<a href="{{ site.baseurl }}/typeclasses.html">Typeclasses</a>
<a href="{{ site.baseurl }}/typeclasses.html">Type Classes</a>
</li>
<li {%if page.section =="data" %} class="active" {% else %} class="inactive" {% endif %}>
<a href="{{ site.baseurl }}/datatypes.html">Data Types</a>
Expand Down
1 change: 0 additions & 1 deletion docs/src/site/colophon.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ integrating them into your own projects.
* [scalacheck](http://scalacheck.org) for property-based testing
* [discipline](https://github.com/typelevel/discipline) for encoding and testing laws
* [kind-projector](https://github.com/non/kind-projector) for type lambda syntax
* [algebra](https://github.com/non/algebra) for algebraic structures shared between Cats, [Spire](https://github.com/non/spire), and [Algebird](https://github.com/twitter/algebird)
* [tut](https://github.com/tpolecat/tut) type-checked example code makes sure that our examples stay in sync with the rest of our source

There are other libraries that aim to foster Functional Programming in the Scala programming language which Cats has a relationship to:
Expand Down
2 changes: 1 addition & 1 deletion docs/src/site/typeclasses.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
layout: default
title: "Typeclasses"
title: "Type Classes"
section: "typeclasses"
---
{% include_relative _tut/typeclasses.md %}
Expand Down
Loading

0 comments on commit e0e098e

Please sign in to comment.