以下是一个最小的、人为的示例:
read :: FilePath -> Aff String
read f = do
log ("File: " <> f) -- (1)
readTextFile UTF8 f -- (2)
Run Code Online (Sandbox Code Playgroud)
我想在发生(1)潜在错误之前进行一些调试日志记录(2)。到目前为止,在 Spago REPL 中执行以下代码适用于成功案例:
$ spago repl
> launchAff_ $ read "test/data/tree/root.txt"
File: test/data/tree/root.txt
unit
Run Code Online (Sandbox Code Playgroud)
(2)问题:如果- file is directory here -出现错误,(1)则似乎根本没有执行:
$ spago repl
> launchAff_ $ read "test/data/tree"
~/purescript-book/exercises/chapter9/.psci_modules/node_modules/Effect.Aff/foreign.js:532
throw util.fromLeft(step);
^
[Error: EISDIR: illegal operation on a directory, read] {
errno: -21,
code: 'EISDIR',
syscall: 'read'
} …Run Code Online (Sandbox Code Playgroud) debugging logging functional-programming purescript visual-studio-code
applicative do notation / adovs. applicative lift via <$>/ mapon the first argument的评估顺序似乎有所不同,而<*>/apply对于其余参数。
至少,这是我到目前为止所读到的内容,以及在下面显示的练习过程中所反映的内容。问题:
ado),尊重测试中的预购断言?可以在此处找到 PureScript by Example 书(第 7 章)中的练习:
3.(中)编写一个函数
traversePreOrder :: forall a m b. Applicative m => (a -> m b) -> Tree a -> m (Tree b)来执行树的前序遍历。[...] Applicative do notation (ado) 是编写此函数的最简单方法。
代数数据类型Tree:
data Tree a
= Leaf …Run Code Online (Sandbox Code Playgroud)