Skip to content

Instantly share code, notes, and snippets.

@Daenyth
Created May 6, 2019 17:36
Show Gist options
  • Save Daenyth/3571088724bea12cd4eda791abadaada to your computer and use it in GitHub Desktop.
Save Daenyth/3571088724bea12cd4eda791abadaada to your computer and use it in GitHub Desktop.
Referential transparency example
// Referential transparency
// An value can be replaced by its definition without changing semantics
// Not RT
def foo() = {
(println("hi"), println("hi"))
}
// bar() != foo()
def bar() = {
val x = println(hi)
(x, x)
}
// Is RT
def fooIO: (IO[Unit], IO[Unit]) = {
(IO(println("hi")), IO(println("hi")))
}
// barIO == fooIO
def barIO: (IO[Unit], IO[Unit]) = {
val x = IO(println(hi))
(x, x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment