Let's explore effect systems in Scala. Imagine a pure functional program that looks something like the following:
val enProgram =
for {
<- write("What's your name? ")
_ <- readLn()
name <- write(s"Hello, ${name}!\n")
_ } yield ()
val esProgram =
for {
<- write("¿Cómo te llamas? ")
_ <- readLn()
name <- write(s"¡Hola, ${name}!\n")
_ } yield ()
val program =
for {
<- readEnv("LANG")
lang <- if (lang.startsWith("es")) {
_
esProgram} else {
enProgram}
} yield ()
This program looks up the $LANG
environment variable,
then performs some simple keyboard and console I/O in the user's
regional language.
This program is itself just a value; it's a representation of a sequence of instructions. On its own, this program doesn't do anything and can't be run. Let's see how to run it using different effect systems: