我不太明白为什么这样做:
module Records where
type Element e = { element :: String, label :: String | e }
type Sel = ( value :: Number, values :: [Number] )
type Select = Element Sel
Run Code Online (Sandbox Code Playgroud)
虽然这样说Cannot unify # * with *
.
module Records where
type Element e = { element :: String, label :: String | e }
type Sel = { value :: Number, values :: [Number] }
type Select = Element Sel
Run Code Online (Sandbox Code Playgroud)
(注意Sel右侧的'()'而不是'{}'.)
我在这里读https://leanpub.com/purescript/read#leanpub-auto-objects-and-rows是forall …
我决定尝试函数式编程和Purescript.阅读"Learn you a Haskell for great good"
并"PureScript by Example"
稍微玩一下代码之后,我认为我可以说我理解了基础知识,但有一件事情让我很烦恼 - 代码看起来非常融洽.通常我经常更换库,在OOP中我可以使用洋葱架构将我自己的代码与库特定的代码分离,但我不知道如何在Purescript中执行此操作.
我试图找到人们如何在Haskell中做到这一点,但我能找到的答案就像"没有人在Haskell中制作过复杂的应用程序,因此没有人知道如何做到这一点"或"你有输入而且你有输出,介于两者之间的只是纯粹的功能".但此时我有一个玩具应用程序,它使用虚拟dom,信号,网络存储,路由器库,每个都有自己的效果和数据结构,所以它听起来不像一个输入和一个输出.
所以我的问题是我应该如何构建我的代码或我应该使用哪些技术,以便我可以更改我的库而不重写我的应用程序的一半?
更新:
建议在主模块中使用多个层并保持效果也很常见,我理解为什么我应该这样做.
这是一个简单的例子,希望能够说明我正在谈论的问题:
btnHandler :: forall ev eff. (MouseEvent ev) => ev -> Eff (dom :: DOM, webStorage :: WebStorage, trace :: Trace | eff) Unit
btnHandler e = do
btn <- getTarget e
Just btnId <- getAttribute "id" btn
Right clicks <- (getItem localStorage btnId) >>= readNumber
let newClicks = clicks + 1
trace $ "Button #" ++ btnId ++ …
Run Code Online (Sandbox Code Playgroud) 我看到了这个问题:
我发现我可以使用purescript-debug打印它,例如使用:
> traceAny {a:1} id
{ a: 1 }
unit
Run Code Online (Sandbox Code Playgroud)
我想知道没有Show
记录的默认实例背后的理由是什么:
> {a:1}
Error found:
in module $PSCI
No type class instance was found for
Data.Show.Show { "a" :: Int
}
Run Code Online (Sandbox Code Playgroud) 有没有办法在PureScript的REPL中重新加载模块或所有模块?如果我对模块进行了更改:quit
,那么import
每次都要使用我正在使用的所有模块.
现在我有一个异步函数,它的工作原理如下:
foo = do
ayncGetNumber "/numberLocation" \a ->
(trace <<< show) a
Run Code Online (Sandbox Code Playgroud)
但是这个回调样式是不可组合的(根据我的理解),我希望它能像这样工作
foo = do
a <- ayncGetNumber "/numberLocation"
(trace <<< show) a
Run Code Online (Sandbox Code Playgroud)
要么
foo = ayncGetNumber "/numberLocation" >>= show >>> trace
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何逃避回调并使其可组合.
我想知道如何在 PureScript 代码中进行注释。
在 Python 中,等价的是:
# Here is a comment
Run Code Online (Sandbox Code Playgroud)
或者 JavaScript
// Another comment
Run Code Online (Sandbox Code Playgroud)
PureScript 中的等效项是什么?
有没有办法做类似的事情
first = {x:0}
second = {x:1,y:1}
both = [first, second]
Run Code Online (Sandbox Code Playgroud)
这样both
被推断为{x::Int | r}
或类似的东西?
我尝试了几件事:
[{x:3}] :: Array(forall r. {x::Int|r}) -- nope
test = Nil :: List(forall r. {x::Int|r})
{x:1} : test -- nope
type X r = {x::Int | r}
test = Nil :: List(X) -- nope
test = Nil :: List(X())
{x:1} : test
{x:1, y:1} : test -- nope
Run Code Online (Sandbox Code Playgroud)
我能想到的一切似乎都告诉我,不支持将这样的记录合并到一个集合中。有点像,函数可以是多态的,但列表不能。这是正确的解释吗?它让我想起了 F#“值限制”问题,虽然我认为这只是因为 CLR 限制而 JS 不应该有这个问题。但也许这无关紧要。
有没有办法声明列表/数组来支持这个?
以下是一个最小的、人为的示例:
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
我有两个我认为等效的类型类和实例定义的实现。PureScript 版本构建时没有错误,但 Haskell 版本失败并出现错误Un-determined variables: e, f
。
我可以在 Haskell 中完成这个工作吗?
哈斯克尔:
class Foo a b c | a b -> c where
newtype Bar a b = Bar (a, b)
instance (Foo a c e, Foo b d f) => Foo (Bar a b) (Bar c d) (Bar e f) where
Run Code Online (Sandbox Code Playgroud)
纯脚本:
class Foo a b c | a b -> c
newtype Bar a b = Bar (Tuple a b)
instance (Foo a c e, Foo b …
Run Code Online (Sandbox Code Playgroud) PureScript中Sproxy有什么用?
在追求中,它被写成
data SProxy (sym :: Symbol)
--| A value-level proxy for a type-level symbol.
Run Code Online (Sandbox Code Playgroud)
purescipt 中的 Symbol 是什么意思?