Skip to content

Commit f5b99e6

Browse files
Add Show.by
1 parent 363b3b0 commit f5b99e6

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

core/src/main/scala/cats/Show.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import java.util.UUID
2525
import scala.collection.immutable.{BitSet, Queue, Seq, SortedMap, SortedSet}
2626
import scala.concurrent.duration.{Duration, FiniteDuration}
2727
import scala.util.Try
28+
import scala.{specialized => sp}
2829

2930
/**
3031
* A type class to provide textual representation. It is meant to be a
@@ -65,6 +66,13 @@ object Show extends ScalaVersionSpecificShowInstances with ShowInstances {
6566
*/
6667
def show[A](f: A => String): Show[A] = f(_)
6768

69+
/**
70+
* Convert an implicit `Show[B]` to a `Show[A]` using the given
71+
* function `f`.
72+
*/
73+
def by[@sp A, @sp B](f: A => B)(implicit ev: Show[B]): Show[A] =
74+
a => ev.show(f(a))
75+
6876
/**
6977
* creates an instance of [[Show]] using object toString
7078
*/

0 commit comments

Comments
 (0)