Skip to content

Commit

Permalink
Make Scaladoc example compile and modernize it
Browse files Browse the repository at this point in the history
  * `release` takes the allocated resource as a param
  * Use IO.println
  * Use Cats Effect's `Random`
  • Loading branch information
igstan committed Jun 26, 2021
1 parent bcffe14 commit 63e6a49
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions kernel/shared/src/main/scala/cats/effect/kernel/Resource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -254,18 +254,27 @@ sealed abstract class Resource[F[_], +A] {
* `both`, for example via `parMapN`:
*
* {{{
* def mkResource(name: String) = {
* val acquire =
* IO(scala.util.Random.nextInt(1000).millis).flatMap(IO.sleep) *>
* IO(println(s"Acquiring $$name")).as(name)
* import scala.concurrent.duration._
* import cats.effect.{IO, Resource}
* import cats.effect.std.Random
* import cats.implicits._
*
* val release = IO(println(s"Releasing $$name"))
* Resource.make(acquire)(release)
* }
* def mkResource(name: String) = {
* val acquire = for {
* n <- Random.scalaUtilRandom[IO].flatMap(_.nextIntBounded(1000))
* _ <- IO.sleep(n.millis)
* _ <- IO.println(s"Acquiring $$name")
* } yield name
*
* val r = (mkResource("one"), mkResource("two"))
* .parMapN((s1, s2) => s"I have \$s1 and \$s2")
* .use(msg => IO(println(msg)))
* def release(name: String) =
* IO.println(s"Releasing $$name")
*
* Resource.make(acquire)(release)
* }
*
* val r = (mkResource("one"), mkResource("two"))
* .parMapN((s1, s2) => s"I have \$s1 and \$s2")
* .use(IO.println(_))
* }}}
*/
def both[B](
Expand Down

0 comments on commit 63e6a49

Please sign in to comment.