-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Added examples for Cokleisli #1993
Changes from 1 commit
fb60456
961c1e3
604b71e
6f6b09e
4c417aa
7adc57d
88abe61
ba82d30
bcbd847
5942abb
3ce0002
ccab819
5d1a68a
1241d54
64231d5
185b44e
139c2c4
e7ee56a
782537f
50dd65e
e4396bf
5401b18
b6cb93d
6a70cea
517dcea
3242791
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,12 +14,13 @@ final case class Cokleisli[F[_], A, B](run: F[A] => B) { self => | |
/** | ||
* Example: | ||
* {{{ | ||
* scala> import cats.Id, cats.implicits._ | ||
* scala> val x : Id[Int] = 42 | ||
* scala> def before(x: Int) = x + 1 | ||
* scala> def after(x: Int) = x - 1 | ||
* scala> val example : Cokleisli[Id,Int,Int] = Cokleisli((f: Id[Int]) => f.extract) | ||
* scala> example.dimap(before)(after) == 42 | ||
* scala> import cats._, data._ | ||
* scala> val f = Cokleisli((xs: NonEmptyList[Int]) => xs.reverse.head) | ||
* f: cats.data.Cokleisli[cats.data.NonEmptyList,Int,Int] = Cokleisli(<function1>) | ||
* scala> def before(x: String) = x.toInt | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hate to be a jerk, but can we use a function that won’t throw. I know this example is safe but using toInt in general is dangerous and I hate to set the example in docs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree |
||
* scala> def after(x: Int) = x.toString | ||
* scala> f.dimap(before)(after).run(NonEmptyList.of("1","2")) | ||
* res0: String = 2 | ||
* }}} | ||
*/ | ||
def dimap[C, D](f: C => A)(g: B => D)(implicit F: Functor[F]): Cokleisli[F, C, D] = | ||
|
@@ -28,12 +29,13 @@ final case class Cokleisli[F[_], A, B](run: F[A] => B) { self => | |
/** | ||
* Example: | ||
* {{{ | ||
* scala> import cats.Id, cats.implicits._ | ||
* scala> val x : Id[Int] = 42 | ||
* scala> def before(x: Int) = x + 1 | ||
* scala> def after(x: Int) = x - 1 | ||
* scala> val example : Cokleisli[Id,Int,Int] = Cokleisli((f: Id[Int]) => f.extract) | ||
* scala> example.lmap(before).rmap(after) == 42 | ||
* scala> import cats._, data._, implicits._ | ||
* scala> val f = Cokleisli((xs: NonEmptyList[Int]) => xs.reverse.head) | ||
* f: cats.data.Cokleisli[cats.data.NonEmptyList,Int,Int] = Cokleisli(<function1>) | ||
* scala> def before(x: String) = x.toInt | ||
* scala> def after(x: Int) = x.toString | ||
* scala> f.lmap(before).rmap(after).run(NonEmptyList.of("1","2")) | ||
* res0: String = 2 | ||
* }}} | ||
*/ | ||
def lmap[C](f: C => A)(implicit F: Functor[F]): Cokleisli[F, C, B] = | ||
|
@@ -42,6 +44,17 @@ final case class Cokleisli[F[_], A, B](run: F[A] => B) { self => | |
def map[C](f: B => C): Cokleisli[F, A, C] = | ||
Cokleisli(f compose run) | ||
|
||
/** | ||
* Example: | ||
* {{{ | ||
* scala> import cats._, data._ | ||
* scala> val sum = Cokleisli((xs: NonEmptyList[Int]) => xs.reduceLeft(_ + _)) | ||
* sum: cats.data.Cokleisli[cats.data.NonEmptyList,Int,Int] = Cokleisli(<function1>) | ||
* | ||
* scala> sum.contramapValue((xs: NonEmptyList[String]) => xs.map(_.toInt)).run(NonEmptyList.of("1","2","3")) | ||
* res4: Int = 6 | ||
* }}} | ||
*/ | ||
def contramapValue[C](f: F[C] => F[A]): Cokleisli[F, C, B] = | ||
Cokleisli(run compose f) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd omit these toString renderings, just like you omitted the output of the defs. The other examples in Cats tend to omit the trivial outputs and focus on the setup and the res0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rossabaker gotcha :)