如何使用 HJScript 或 HJavaScript 创建数组文字?
在 HJavaScript 中有 Array
类型,但我看不到一种构造可以翻译为 JS 的文字的方法,例如 [1,2,3].如果不需要的话,我不想创建一个
new Array()
然后将项目推入其中。
理想情况下,我正在寻找像 array :: [t] -> 这样的函数数组 t
。
我可以使用 JConst 来实现数组,但这似乎是对一些应该简单的事情的黑客攻击。我还可以使用上面的 create-and-push 方法来实现 array,但这也不是很好。
这里是push的数组
;不太好。
array :: [Exp a] -> JS (JArray a)
array xs = do
arr <- new Array ()
mapM_ (`push` arr) xs
return arr
In HJavaScript there is the Array
type, but I can't see a way of constructing a literal that would translate, for example, to JS as [1,2,3]
. I don't want to have to create a new Array()
and then push items into it, if I don't have to.
Ideally I'm after a function like array :: [t] -> Array t
.
I could possibly use JConst
to implement array
, but it seems like a hack for something that should be straight-forward. I could also do the create-and-push method above to implement array
, this is also not great, though.
Here is array
by pushing; not so great.
array :: [Exp a] -> JS (JArray a)
array xs = do
arr <- new Array ()
mapM_ (`push` arr) xs
return arr
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题是我第一次听说HJscript。简要查看文档,我看不到任何方法来制作像
[1,2,3]
这样的简单数组文字。但是,我确实看到了一种调用函数的方法,并注意到[1,2,3] = Array(1,2,3)
。事实上,我敢打赌解释者将前者视为后者的糖。因此,如果您可以调用函数,您就可以构造文字。This question is the first I've heard of HJscript. Briefly looking at the docs, I can't see any way to make a simple array literal like
[1,2,3]
. But, I do see a way to call functions, and note that[1,2,3] = Array(1,2,3)
. In fact, I'll bet interpreters treat the former as sugar for the latter. So if you can call functions, you can construct literals.