Created
May 6, 2019 17:36
-
-
Save Daenyth/3571088724bea12cd4eda791abadaada to your computer and use it in GitHub Desktop.
Referential transparency example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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