Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
magnus-madsen committed Jan 12, 2025
1 parent ef6c5a3 commit 1b73557
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions src/next-steps.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@
We are now ready write our first real program!

We will write a simple variant of the venerable wordcount (`wc`) program from
UNIX. We will use the opportunity to illustrate how to use algebraic effects in
Flix.
UNIX.

Please follow the following steps:

1. Create a new empty folder (e.g. `wc`)
2. Initialize an empty project in that folder (e.g. `java -jar flix.jar init`)
3. Put the following code into `src/Main.flix`:
We will use the opportunity to illustrate how to use algebraic effects in Flix.

```flix
def wordCount(file: String): Unit \ {Console, FileReadWithResult} = {
def wc(file: String): Unit \ {Console, FileReadWithResult} = {
match FileReadWithResult.readLines(file) {
case Ok(lines) =>
let totalLines = List.length(lines);
Expand All @@ -29,36 +24,35 @@ def numberOfWords(s: String): Int32 =
def main(): Unit \ IO =
run {
wordCount("Main.flix")
wc("Main.flix")
} with Console.runWithIO
with FileReadWithResult.runWithIO
```

The program works as follows:

We define a `wordCount` function that takes a filename and reads all lines from
the file using the algebraic effect `FileReadWithResult`.
We define a `wc` function that takes a filename and reads all lines from the
file using the algebraic effect `FileReadWithResult`.

If the file is successfully read, we calculate:

- The number of lines using `List.length`.
- The number of words by summing the results of applying the `numberOfWords`
helper function to each line. This function computes the number of words in a
given string.
- The number of words by summing the results of applying `numberOfWords` to each
line.

The results are printed to the terminal using the `Console` algebraic effect.

If the file cannot be read, an error message is printed to the terminal using
the same effect.

The `wordCount` function's type and effect signature specifies the `{Console,
The `wc` function's type and effect signature specifies the `{Console,
FileReadWithResult}` effect set, indicating these effects are required.
Conceptually, the function is pure except for these specified effects, which
must be handled by the caller.
Conceptually, the function is pure except for these effects, which must be
handled by the caller.

Finally, the `main` function calls `wordCount` with a fixed filename. Since
`wordCount` uses the `Console` and `FileReadWithResult` effects, we must provide
their implementations. This is achieved using the `run-with` construct, where we
The `main` function calls `wc` with a fixed filename. Since `wc` uses the
`Console` and `FileReadWithResult` effects, we must provide their
implementations. This is achieved using the `run-with` construct, where we
specify the default handlers `Console.runWithIO` and
`FileReadWithResult.runWithIO`.

0 comments on commit 1b73557

Please sign in to comment.