我有一个 API 强制我以以下形式返回 JSON
{ Field1: "something"
, Field2: 12 }
Run Code Online (Sandbox Code Playgroud)
然而,到目前为止,我还没有在 Purescript 中对此进行建模。
据我了解,purescript 语法看到大写字段名称并认为它是没有字段名称的字段类型。所以我并不直接编码像这样的函数
test :: Number -> { Field1 :: String, Field2 :: Number }
Run Code Online (Sandbox Code Playgroud)
在不使用外部 javascript 函数将对象更改为其他内容的情况下,是否可以在 purescript 中构造具有大写字段名称的记录?如果是这样,怎么办?
我刚刚开始通过示例学习purescript ,但是我在第2章的末尾使用了grunt项目模板.我按照说明操作,但是当我运行grunt命令时,我收到此错误:
/Users/chad/playground/purescript/tests/tmp/node_modules/Control.Monad.Eff.Random/index.js:8
return Math.random();
^
TypeError: undefined is not a function
at Object.random (/Users/chad/playground/purescript/tests/tmp/node_modules/Control.Monad.Eff.Random/index.js:8:17)
at __do (/Users/chad/playground/purescript/tests/tmp/node_modules/Test.QuickCheck/index.js:177:52)
at Object.__do (/Users/chad/playground/purescript/tests/tmp/node_modules/Main/index.js:19:201)
at Object.<anonymous> (/Users/chad/playground/purescript/tests/tmp/index.js:1:79)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
Warning: -> error 1 /Users/chad/playground/purescript/tests/tmp/index.js (125ms) Use --force to continue.
Aborted due to warnings.
Run Code Online (Sandbox Code Playgroud)
我通过深入研究代码发现,模块Math被导入为/output/Control.Monad.Eff.Random/index.jsas
var Math = require("Math");
function random() {
return Math.random();
}
;
Run Code Online (Sandbox Code Playgroud)
如果我删除导入,让Math是原生的并且index.js简单地运行main …
我试图通过Node.FS.Aff.readTextFile获取我从文件中读取的一些文件内容,因此使用异步效果.
但是我的问题更为笼统.
myFile::forall r. String -> Aff ( fs :: FS | r) (Either Error String)
myFile file = attempt $ readTextFile Node.Encoding.UTF8 file
Run Code Online (Sandbox Code Playgroud)
所以我想得到Left或者Right价值.如果它在哪里Eff而不是Aff我可以使用
let x = unsafePerformEff $ myFile "someFile.txt"
Run Code Online (Sandbox Code Playgroud)
谢谢
我刚刚开始学习PureScript效果,并且我试图制作一个具有EXCEPTION效果的函数.
lengthGt5 :: forall eff. String -> Eff (err :: EXCEPTION | eff) String
lengthGt5 a = if (length a <= 5)
then throwException $ error "Word is not the right length!"
else a
main = do
word <- catchException handleShortWord (lengthGt5 "test")
log word
where
handleShortWord err = do
log (message err)
return "Defaut::casserole"
Run Code Online (Sandbox Code Playgroud)
当我尝试运行它时,我收到以下错误
无法匹配类型
String
with type
Eff
( err :: EXCEPTION
| eff0
)
String
Run Code Online (Sandbox Code Playgroud)
我知道lengthGt5需要在非异常的情况下返回一个包装在Eff中的String,但是我不知道如何在值周围创建一个"空效果包装器" a.我在考虑这个吗?
BindPrelude中类的定义是:
class Apply m <= Bind m where
bind :: forall a b. m a -> (a -> m b) -> m b
Run Code Online (Sandbox Code Playgroud)
它可以作为一个函数读取,它接受两个输入参数,monadic context(m a)和function(a -> m b)中的值,并在monadic context(m b)中返回一个值.
Bind部分应用函数的实例定义为:
instance bindFn :: Bind ((->) r) where
bind m f x = f (m x) x
Run Code Online (Sandbox Code Playgroud)
这是一个采用三个参数的函数.那个类型检查怎么样?
如果我尝试用m a更具体的类型替换我得到(如果我错了,请纠正我):
(((->) ??) a) -> (a -> (((->) ??) b)) -> (((->) ??) b)
Run Code Online (Sandbox Code Playgroud)
这相当于
(?? …Run Code Online (Sandbox Code Playgroud) 我正在尝试从Haskell服务器获取一些JSON数据,但我遇到了Respondeable实例,以及一般的Affjax.我已经使用Data.Argonaut.Generic.Aeson(GA)定义了EncodeJson + DecodeJson,但我无法弄清楚如何将其与Respondeable实例相匹配并且它来自响应函数.
它给了我错误"无法匹配类型外部与类型Json"但是可以重用我的decodeJson实例而不必手动创建任何其他东西吗?也许通过创建一个IsForeign实例,但使用GA.decodeJson?我只是不确定如何去做.我已经看到它是如何在https://github.com/purescript/purescript-foreign/blob/master/examples/Complex.purs手动完成的,但我有复杂的类型需要与我的Haskell JSON输出相匹配,手动完成这将是一个巨大的痛苦.
我正在使用purescript 10.7,Affjax 3.02和argonaut 2.0.0,以及argonaut-generic-codecs 5.1.0.谢谢!
testAffjax :: forall eff. Aff (ajax :: AJAX | eff) (Answer)
testAffjax = launchAff do
res <- affjax $ defaultRequest { url = "/", method = Left GET }
pure res.response
data Answer = Answer {
_answer :: String
, _isCorrect :: Boolean
, _hint :: String
}
{- PROBLEM -}
instance respondableAnswer :: Respondable Answer where
responseType = Tuple Nothing JSONResponse
fromResponse = GA.decodeJson {- Error here …Run Code Online (Sandbox Code Playgroud) 在purescript-free程序包中,有一个定义此解释器的代码示例:
teletypeN :: forall eff. NaturalTransformation TeletypeF (Eff (console :: CONSOLE | eff))
teletypeN (PutStrLn s a) = const a <$> log s
teletypeN (GetLine k) = pure (k "fake input")
Run Code Online (Sandbox Code Playgroud)
如何定义和运行返回类型为Array Int或的其他解释器State String Int?
我试图基于数据数组创建记录,该函数如下所示:
type Address = {
street :: String,
city :: String,
state :: String
}
convertToAddress :: Array String -> Maybe Address
convertToAddress [street, city, state] = Just (Address { street: street, city: city, state: state })
convertToAddress _ = Nothing
Run Code Online (Sandbox Code Playgroud)
在这里,我试图使用Address值构造函数创建Address类型的Record,但是在编译时会引发错误:
Unknown data constructor Address
Run Code Online (Sandbox Code Playgroud) 我是一个纯粹的PureScript新手,需要一些帮助来弄清楚为什么用Aff monad模拟的FFI功能似乎对我不起作用.
预期的行为是在1000ms后将消息"keyMessage"记录到控制台.
相反,程序只是在以下输出后无限期挂起:
Compiling Main
* Build successful.
Waiting for message...
Run Code Online (Sandbox Code Playgroud)
Main.purs:
module Main where
import Prelude
import Control.Monad.Aff (Aff, Fiber, launchAff)
import Control.Monad.Aff.Console (log)
import Control.Monad.Eff (Eff, kind Effect)
import Control.Monad.Eff.Console (CONSOLE)
main :: forall e. Eff (console :: CONSOLE, to :: TIMEOUT | e) (Fiber (console :: CONSOLE, to :: TIMEOUT | e) Unit)
main = launchAff do
log "Waiting for message..."
m <- message "key"
log m
foreign import data TIMEOUT :: Effect
foreign import message …Run Code Online (Sandbox Code Playgroud) 我认为埋没到列出的语言源代码对我来说太过分了.但有没有人能够用简单的术语解释事情是如何发生的?
我的意思是,最终不可变数据仍将是javascript的数据.或者编译代码包含非std数据结构,例如用于不可变数组的'a,b,c'字符串
purescript ×10
monads ×2
record ×2
adt ×1
argonaut ×1
effects ×1
elm ×1
ffi ×1
free-monad ×1
ghcjs ×1
immutability ×1
uppercase ×1