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

Enrich return type for Representable.apply and add doctests #2426

Merged
merged 1 commit into from
Sep 5, 2018
Merged
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
42 changes: 41 additions & 1 deletion core/src/main/scala/cats/Representable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,41 @@ trait Representable[F[_]] extends Serializable {

/**
* Create a function that "indexes" into the `F` structure using `Representation`
*
* Example:
* {{{
* scala> import cats.implicits._
*
* scala> type Pair[A] = (A, A)
*
* scala> val indexed: Boolean => String = Representable[Pair].index(("foo", "bar"))
*
* scala> indexed(true)
* res0: String = foo
*
* scala> indexed(false)
* res1: String = bar
* }}}
*/
def index[A](f: F[A]): Representation => A

/**
* Reconstructs the `F` structure using the index function
*
* Example:
* {{{
* scala> import cats.implicits._
*
* scala> type Pair[A] = (A, A)
*
* scala> val f: Boolean => String = {
* | case true => "foo"
* | case false => "bar"
* | }
*
* scala> f.tabulate[Pair]
* res0: Pair[String] = (foo,bar)
* }}}
*/
def tabulate[A](f: Representation => A): F[A]
}
Expand Down Expand Up @@ -69,8 +99,18 @@ object Representable {

/**
* Summon the `Representable` instance for `F`
*
* Example:
* {{{
* scala> import cats.implicits._
*
* scala> type Pair[A] = (A, A)
*
* scala> Representable[Pair].index(("foo", "bar"))(false)
* res0: String = bar
* }}}
*/
def apply[F[_]](implicit ev: Representable[F]): Representable[F] = ev
def apply[F[_]](implicit ev: Representable[F]): Representable.Aux[F, ev.Representation] = ev

/**
* Derives a `Monad` instance for any `Representable` functor
Expand Down