Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add doctests for Semigroup, Group, and Monoid #2523

Merged
merged 4 commits into from
Sep 26, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ lazy val kernel = crossProject(JSPlatform, JVMPlatform, NativePlatform)
.settings(includeGeneratedSrc)
.jsSettings(commonJsSettings)
.jvmSettings(commonJvmSettings ++ mimaSettings("cats-kernel"))
.settings(libraryDependencies += "org.scalacheck" %%% "scalacheck" % scalaCheckVersion(scalaVersion.value) % "test")


lazy val kernelJVM = kernel.jvm
Expand Down
16 changes: 16 additions & 0 deletions kernel/src/main/scala/cats/kernel/Group.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,29 @@ trait Group[@sp(Int, Long, Float, Double) A] extends Any with Monoid[A] {
* Find the inverse of `a`.
*
* `combine(a, inverse(a))` = `combine(inverse(a), a)` = `empty`.
*
* Example:
* {{{
* scala> import cats.implicits._
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that cats.implicits is only defined in cats-core, not in cats-kernel, so importing it here won't work.

*
* scala> Group[Int].inverse(5)
* res0: Int = -5
* }}}
*/
def inverse(a: A): A

/**
* Remove the element `b` from `a`.
*
* Equivalent to `combine(a, inverse(b))`
*
* Example:
* {{{
* scala> import cats.implicits._
*
* scala> Group[Int].remove(5, 2)
* res0: Int = 3
* }}}
*/
def remove(a: A, b: A): A = combine(a, inverse(b))

Expand Down
44 changes: 44 additions & 0 deletions kernel/src/main/scala/cats/kernel/Monoid.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,50 @@ trait Monoid[@sp(Int, Long, Float, Double) A] extends Any with Semigroup[A] {

/**
* Return the identity element for this monoid.
*
* Example:
* {{{
* scala> import cats.implicits._
*
* scala> Monoid[String].empty
* res0: String = ""
*
* scala> Monoid[Int].empty
* res1: Int = 0
* }}}
*/
def empty: A

/**
* Tests if `a` is the identity.
*
* Example:
* {{{
* scala> import cats.implicits._
*
* scala> Monoid[String].isEmpty("")
* res0: Boolean = true
*
* scala> Monoid[String].isEmpty("something")
* res1: Boolean = false
* }}}
*/
def isEmpty(a: A)(implicit ev: Eq[A]): Boolean =
ev.eqv(a, empty)

/**
* Return `a` appended to itself `n` times.
*
* Example:
* {{{
* scala> import cats.implicits._
*
* scala> Monoid[String].combineN("ha", 3)
* res0: String = "hahaha"
*
* scala> Monoid[String].combineN("ha", 0)
* res1: String = ""
* }}}
*/
override def combineN(a: A, n: Int): A =
if (n < 0) throw new IllegalArgumentException("Repeated combining for monoids must have n >= 0")
Expand All @@ -31,6 +64,17 @@ trait Monoid[@sp(Int, Long, Float, Double) A] extends Any with Semigroup[A] {

/**
* Given a sequence of `as`, sum them using the monoid and return the total.
*
* Example:
* {{{
* scala> import cats.implicits._
*
* scala> Monoid[String].combineAll(List("One ", "Two ", "Three"))
* res0: String = "One Two Three"
*
* scala> Monoid[String].combineAll(List.empty)
* res1: String = ""
* }}}
*/
def combineAll(as: TraversableOnce[A]): A =
as.foldLeft(empty)(combine)
Expand Down
35 changes: 34 additions & 1 deletion kernel/src/main/scala/cats/kernel/Semigroup.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cats.kernel

import scala.{ specialized => sp }
import scala.{specialized => sp}
import scala.annotation.tailrec
/**
* A semigroup is any set `A` with an associative operation (`combine`).
Expand All @@ -9,11 +9,33 @@ trait Semigroup[@sp(Int, Long, Float, Double) A] extends Any with Serializable {

/**
* Associative operation which combines two values.
*
* Example:
* {{{
* scala> import cats.implicits._
*
* scala> Semigroup[String]("Hello ", "World!")
* res0: String = "Hello World!"
*
* scala> Semigroup[Option[Int]].combine(None, Some(1))
* res1: Option[Int] = Some(1)
* }}}
*/
def combine(x: A, y: A): A

/**
* Return `a` combined with itself `n` times.
*
* Example:
* {{{
* scala> import cats.implicits._
*
* scala> Semigroup[Int].combineN(1, 10)
* res0: Int = 10
*
* scala> Semigroup[String].combineN("ha", 3)
* res1: String = "hahaha"
* }}}
*/
def combineN(a: A, n: Int): A =
if (n <= 0) throw new IllegalArgumentException("Repeated combining for semigroups must have n > 0")
Expand All @@ -35,6 +57,17 @@ trait Semigroup[@sp(Int, Long, Float, Double) A] extends Any with Serializable {
* Given a sequence of `as`, combine them and return the total.
*
* If the sequence is empty, returns None. Otherwise, returns Some(total).
*
* Example:
* {{{
* scala> import cats.implicits._
*
* scala> Semigroup[String].combineAllOption(List("One ", "Two ", "Three"))
* res0: Option[String] = Some("One Two Three")
*
* scala> Semigroup[String].combineAllOption(List.empty)
* res1: Option[String] = None
* }}}
*/
def combineAllOption(as: TraversableOnce[A]): Option[A] =
cats.kernel.compat.TraversableOnce.reduceOption(as, combine)
Expand Down