diff --git a/src/monadic-for-yield.md b/src/monadic-for-yield.md index 3fd22246..a9a3212f 100644 --- a/src/monadic-for-yield.md +++ b/src/monadic-for-yield.md @@ -61,12 +61,14 @@ Similarly, we can use `forM` to work with the `Result[e, t]` data type. For example: ```flix -def main(): Result[String, Unit] \ IO = +use Sys.Console + +def main(): Result[String, Unit] \ IO = println("Please enter your first name, last name, and age:"); forM ( - fstName <- Console.readLine(); - lstName <- Console.readLine(); - ageLine <- Console.readLine(); + fstName <- Console.readln(); + lstName <- Console.readln(); + ageLine <- Console.readln(); ageNum <- Int32.parse(10, ageLine) ) yield { println("Hello ${lstName}, ${fstName}."); @@ -75,7 +77,7 @@ def main(): Result[String, Unit] \ IO = ``` Here `main` prompts the user to enter their first name, last name, and age. Each -call to `Console.readLine` returns a `Result[String, String]` value which is +call to `Console.readln` returns a `Result[String, String]` value which is either an error or the input string. Thus the local variables `fstName`, `lstName`, and `ageLine` are `String`s. We parse `ageLine` into an `Int32` using `Int32.parse`, which returns a `Result[String, Int32]` value. If every operation diff --git a/src/next-steps.md b/src/next-steps.md index 1a012bbb..4f56eb88 100644 --- a/src/next-steps.md +++ b/src/next-steps.md @@ -8,21 +8,23 @@ UNIX. We will use the opportunity to illustrate how to use algebraic effects in Flix. ```flix +use Sys.Console + def wc(file: String): Unit \ {Console, FileReadWithResult} = { match FileReadWithResult.readLines(file) { - case Ok(lines) => + case Ok(lines) => let totalLines = List.length(lines); let totalWords = List.sumWith(numberOfWords, lines); Console.println("Lines: ${totalLines}, Words: ${totalWords}") - case Err(_) => + case Err(_) => Console.println("Unable to read file: ${file}") } } -def numberOfWords(s: String): Int32 = +def numberOfWords(s: String): Int32 = s |> String.words |> List.length -def main(): Unit \ IO = +def main(): Unit \ IO = run { wc("Main.flix") } with Console.runWithIO diff --git a/src/printing-to-stdout.md b/src/printing-to-stdout.md index 20e82fbe..bac41fe4 100644 --- a/src/printing-to-stdout.md +++ b/src/printing-to-stdout.md @@ -21,15 +21,17 @@ The `println` function is rightfully effectful, hence it cannot be called from a pure function. To debug a pure function, use the builtin [debugging facilities](./debugging.md). -## The Console Module +## The Console Effect -The `Console` module defines additional functions for reading from or writing to -the terminal: +The `Console` effect defines operations for reading from and writing to the +terminal: ```flix -mod Console { - def print(x: a): Unit \ IO with ToString[a] - def println(x: a): Unit \ IO with ToString[a] - def readLine(): Result[String, String] \ IO Impure +eff Console { + def readln(): String + def print(s: String): Unit + def eprint(s: String): Unit + def println(s: String): Unit + def eprintln(s: String): Unit } ```