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 some doctest examples for SemigroupK/MonoidK #2077

Merged
merged 1 commit into from
Dec 7, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions core/src/main/scala/cats/MonoidK.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ import simulacrum.typeclass

/**
* Given a type A, create an "empty" F[A] value.
*
* Example:
* {{{
* scala> import cats.implicits._
* scala> MonoidK[List].empty[Long]
* res0: List[Long] = List()
* }}}
*/
def empty[A]: F[A]

Expand Down
27 changes: 27 additions & 0 deletions core/src/main/scala/cats/SemigroupK.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,45 @@ import simulacrum.typeclass

/**
* Combine two F[A] values.
*
* Example:
* {{{
* scala> import cats.implicits._
* scala> SemigroupK[List].combineK(List(1, 2), List(3, 4))
* res0: List[Int] = List(1, 2, 3, 4)
* }}}
*/
@simulacrum.op("<+>", alias = true)
def combineK[A](x: F[A], y: F[A]): F[A]

/**
* Given a type A, create a concrete Semigroup[F[A]].
*
* Example:
* {{{
* scala> import cats.implicits._
* scala> val s: Semigroup[List[Int]] = SemigroupK[List].algebra[Int]
* }}}
*/
def algebra[A]: Semigroup[F[A]] =
new Semigroup[F[A]] {
def combine(x: F[A], y: F[A]): F[A] = self.combineK(x, y)
}

/**
* "Compose" with a `G[_]` type to form a `SemigroupK` for `λ[α => F[G[α]]]`.
* Note that this universally works for any `G`, because the "inner" structure
* isn't considered when combining two instances.
*
* Example:
* {{{
* scala> import cats.implicits._
* scala> type ListOption[A] = List[Option[A]]
* scala> val s: SemigroupK[ListOption] = SemigroupK[List].compose[Option]
* scala> s.combineK(List(Some(1), None, Some(2)), List(Some(3), None))
* res0: List[Option[Int]] = List(Some(1), None, Some(2), Some(3), None)
* }}}
*/
def compose[G[_]]: SemigroupK[λ[α => F[G[α]]]] =
new ComposedSemigroupK[F, G] {
val F = self
Expand Down