[Ometa] Array concatenation in OMeta/JS
Page
pjgazzard at googlemail.com
Tue Oct 25 06:31:16 PDT 2011
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 =3D "str":s ":" value:v -> {obj =3D {}; obj[s[1]] =3D 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> 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 =3D 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.
>
> ______________________________**_________________
> OMeta mailing list
> OMeta at vpri.org
> http://vpri.org/mailman/**listinfo/ometa<http://vpri.org/mailman/listinfo=
/ometa>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://vpri.org/pipermail/ometa/attachments/20111025/9aa33f3f/attachme=
nt.htm
More information about the OMeta
mailing list