Skip to content

Commit cb8389d

Browse files
Zelenyamilesfrain
authored andcommitted
Fix aff main example in chapter 9
1 parent 8dd2375 commit cb8389d

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

exercises/chapter9/test/Copy.purs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,27 @@ module Test.Copy where
33
-- ANCHOR: copyFile
44
import Prelude
55
import Data.Either (Either(..))
6-
import Effect.Aff (Aff, attempt, message)
6+
import Effect.Aff (Aff, attempt, message, launchAff_)
7+
import Effect (Effect)
78
import Effect.Class.Console (log)
89
import Node.Encoding (Encoding(..))
910
import Node.FS.Aff (readTextFile, writeTextFile)
1011
import Node.Path (FilePath)
1112

12-
copyFile :: FilePath -> FilePath -> Aff Unit
13-
copyFile file1 file2 = do
14-
my_data <- readTextFile UTF8 file1
15-
writeTextFile UTF8 file2 my_data
13+
main :: Effect Unit
14+
main = launchAff_ program
1615

17-
main :: Aff Unit
18-
main = do
16+
program :: Aff Unit
17+
program = do
1918
result <- attempt $ copyFile "file1.txt" "file2.txt"
2019
case result of
2120
Left e -> log $ "There was a problem with copyFile: " <> message e
2221
_ -> pure unit
22+
23+
copyFile :: FilePath -> FilePath -> Aff Unit
24+
copyFile file1 file2 = do
25+
my_data <- readTextFile UTF8 file1
26+
writeTextFile UTF8 file2 my_data
2327
-- ANCHOR_END: copyFile
2428

2529
-- Main is unused, and is only linked to in text

text/chapter9.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ The `Aff` monad in PureScript offers similar ergonomics of JavaScript's `async`/
5252
{{#include ../exercises/chapter9/test/Copy.purs:copyFile}}
5353
```
5454

55+
Note that we have to use `launchAff_` to convert the `Aff` to `Effect` because `main` must be `Effect Unit`.
56+
5557
It is also possible to re-write the above snippet using callbacks or synchronous functions (for example with `Node.FS.Async` and `Node.FS.Sync` respectively), but those share the same downsides as discussed earlier with JavaScript, and so that coding style is not recommended.
5658

5759
The syntax for working with `Aff` is very similar to working with `Effect`. They are both monads, and can therefore be written with do notation.

0 commit comments

Comments
 (0)