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

Added examples for Cokleisli #1993

Merged
merged 26 commits into from
Oct 29, 2017
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
fb60456
Example for beginners to use Arrow composition
Oct 7, 2017
961c1e3
Merge branch 'master' of https://github.com/typelevel/cats
Oct 7, 2017
604b71e
Example for beginners to use Arrow composition
Oct 7, 2017
6f6b09e
Got rid of whitespace at EOL
Oct 8, 2017
4c417aa
Merge branch 'master' of https://github.com/raymondtay/cats
raymondtay Oct 9, 2017
7adc57d
Added type signatures
raymondtay Oct 9, 2017
88abe61
Merge branch 'master' of https://github.com/typelevel/cats
Oct 10, 2017
ba82d30
Merge branch 'master' of https://github.com/raymondtay/cats
Oct 10, 2017
bcbd847
Got rid of the unused import that is borking the build
raymondtay Oct 10, 2017
5942abb
Merge branch 'master' of https://github.com/typelevel/cats
raymondtay Oct 11, 2017
3ce0002
Merge branch 'master' of https://github.com/typelevel/cats
raymondtay Oct 12, 2017
ccab819
Merge branch 'master' of https://github.com/typelevel/cats
raymondtay Oct 16, 2017
5d1a68a
Merge branch 'master' of https://github.com/typelevel/cats
raymondtay Oct 16, 2017
1241d54
Merge branch 'master' of https://github.com/typelevel/cats
raymondtay Oct 18, 2017
64231d5
Merge branch 'master' of https://github.com/raymondtay/cats
raymondtay Oct 18, 2017
185b44e
Merge branch 'master' of https://github.com/typelevel/cats
raymondtay Oct 23, 2017
139c2c4
Merge branch 'master' of https://github.com/raymondtay/cats
raymondtay Oct 23, 2017
e7ee56a
Merge branch 'master' of https://github.com/typelevel/cats
raymondtay Oct 24, 2017
782537f
Added examples for dimap,lmap,rmap for Cokleisli
raymondtay Oct 25, 2017
50dd65e
Exchanged the Cokleisli Id examples with NonEmptyList
raymondtay Oct 27, 2017
e4396bf
Revert "Exchanged the Cokleisli Id examples with NonEmptyList"
raymondtay Oct 27, 2017
5401b18
Exchanged the Cokleisli examples from 'Id' to use 'NonEmptyList' instead
raymondtay Oct 27, 2017
b6cb93d
Merge branch 'master' of https://github.com/typelevel/cats
raymondtay Oct 27, 2017
6a70cea
Removed redundant renderings
raymondtay Oct 28, 2017
517dcea
This is probably safer.
raymondtay Oct 29, 2017
3242791
doh.have not quite woke up yet.
raymondtay Oct 29, 2017
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
37 changes: 25 additions & 12 deletions core/src/main/scala/cats/data/Cokleisli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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>)
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rossabaker gotcha :)

* scala> def before(x: String) = x.toInt
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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] =
Expand All @@ -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] =
Expand All @@ -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)

Expand Down