Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create array literals with HJScript or indeed HJavaScript?

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
like image 861
Christopher Done Avatar asked May 07 '11 17:05

Christopher Done


1 Answers

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.

like image 94
luqui Avatar answered Oct 12 '22 16:10

luqui