Skip to content

Commit

Permalink
Add some doctest examples for SemigroupK/MonoidK (#2077)
Browse files Browse the repository at this point in the history
  • Loading branch information
ceedubs authored and kailuowang committed Dec 7, 2017
1 parent 084b984 commit bb64ef9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
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

0 comments on commit bb64ef9

Please sign in to comment.