Skip to content

Instantly share code, notes, and snippets.

@loicdescotte

loicdescotte/RT.scala

Last active Nov 20, 2019
Embed
What would you like to do?
Referential Transparency example with Try and IO
import scala.util._
import cats.effect._
//See https://impurepics.com/posts/2018-04-01-referential-transparency.html for RT definition
println("-----------Try--------------")
println("-----------This will print only 1 line : --------------")
val t = Try(println(1+1))
for {
a <- t
b <- t
} yield (a, b)
println("-----------This will print 2 lines : --------------")
for {
a <- Try(println(1+1))
b <- Try(println(1+1))
} yield (a, b)
println("----------IO--------------")
val io = IO.delay(println(1+1))
val program = for {
a <- io
b <- io
} yield (a, b)
val program2 = for {
a <- IO.delay(println(1+1))
b <- IO.delay(println(1+1))
} yield (a, b)
println("-----------This will print 2 lines : --------------")
program.unsafeRunSync
println("-----------This will print 2 lines : --------------")
program2.unsafeRunSync
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment