Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/monadic-for-yield.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}.");
Expand All @@ -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
Expand Down
10 changes: 6 additions & 4 deletions src/next-steps.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 9 additions & 7 deletions src/printing-to-stdout.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```