[Ometa] Array concatenation in OMeta/JS

Loup Vaillant l at loup-vaillant.fr
Tue Oct 25 08:35:17 PDT 2011


It Works!  Thank you very much, that was most helpful.
I think the JSON parser is finished, I left it here:
http://www.tinlizzie.org/ometa-js/#JSON

I didn't think about the string conversion, that really confused me.

I have chosen  [e].concat(r) over {r.unshift(e); r} because of its more
functional feel (efficiency be dammed).  Thanks for telling me about
both, though.

I followed your advice for object creation, but in the 'object' rule
instead of the 'key_value' rule.  I note that I cannot use the for_each
style loop ("for (var i in array)") without looping over the methods of
the array themselves.  It looks like arrays are just objects.  Hmm, I
can see where Lua tables come from, now.

Thanks again,
Loup.



From: Page <pjgazzard at googlemail.com>

> When you do + on two arrays it seems to convert them both to strings and
> then concatenate those strings, so "[true][[num, 42], [str, foo]]" as a
> string rather than an array, to do concatenation you would want to do
> [e].concat(r), in this case you could also do {r.unshift(e); r} to add e
> to the start of the array (returns new length) then return r afterwards
> for ometa.
> There is also the option to do [e, r] to put them into an array, but the
> second element would just be the r array (but I don't think that's what
> you want?)
> For the javascript object you can use:
> key_value   = "str":s ":" value:v -> {obj = {}; obj[s[1]] = v[1]; obj},
> This creates obj as a javascript object, then it assigns the value to
> the key and then returns the object, we can't do it in one as you cannot
> have dynamic keys in a static object initialisation.  I use the braces
> {} as it allows you to put multiple javascript statements and it will
> just use the return value of the final one.
>
> I hope this is helpful,
> Pagan
>
> On 25 October 2011 13:46, Loup Vaillant <l at loup-vaillant.fr
> <mailto:l at loup-vaillant.fr>> wrote:
>
>     (sorry if there is a duplicate, I originally sent it from a wrong
>     address)
>
>     Hello,
>
>     I am currently learning OMeta.  While doing it, I am I am trying to
>     make a JSON parser: http://www.tinlizzie.org/__ometa-js/#JSON
>     <http://www.tinlizzie.org/ometa-js/#JSON>
>
>     At the bottom of the source, you will see that I hit a snag:  when I
>     try to print an array, I get
>
>       [true][[num, 42], [str, foo]]
>
>     instead of
>
>       [[true], [num, 42], [str, foo]]
>
>     Apparently, the culprit is the 'listOf' rule:
>
>       listOf :p = apply(p):e ("," apply(p))*:r -> ([e] + r)
>
>     My (weak) understanding of JavaScript tells me that the '+' operator is
>     supposed to concatenate the two arrays.  Apparently, OMeta/JS does
>     something different.
>
>     So my questions are:
>
>       - What does the '+' operator actually return when I apply it to two
>        arrays?
>
>       - Whow can I make an array out of an element and an array?
>
>       - (bonus) I will have similar problems with the object rule.  I don't
>        want an array of key:value pairs, I want a JavaScript object, as
>        JSON was originally intended (so I can claim I have implemented a
>        full JSON parser).
>
>     Thanks,
>     Loup.




More information about the OMeta mailing list