8 ms·
It is if you want to differentiate between a list of lists and a map. Maps are not natively supported in s-expressions. So there is a need to create a sub-lang
by rdtsc 14y ago
It is if you want to differentiate between a list of lists and a map. Maps are not natively supported in s-expressions.
So there is a need to create a sub-language to handle special data structures.
I have done this as well. But I use a special namespace '#dict' for example. So it would be
(#dict (k1 v1) (k2 v2))
This way there is less of a chance of collision between data and this sub-language. It also allows for extension, so for example instead of dict you can use #classname to say persist a perticular object of a given class name (json doesn't allow this, there would have to add a '__classname__' key to the object for example.
Another way of doing it is using a flat list and just using an alternation with keys being prefixed by some special character:
(:k1 v1 :k2 v2)
I think Clojure and some lisps do that...
- groovy2shoes 14y agoClojure s-expressions actually have syntax for vectors, maps, and sets (in addition to the ones mentioned in TFA): {:k1 v1, :k2 v2} ; curly braces for maps [v1 v2 v3] ; square brackets for vectors #{v1 v2 v3} ; pound-curly for sets Clojure's reader also provides some mechanisms for adding readable values, which is awesome for serialization and interchange. There's more info here: http://clojure.org/reader http://clojure.org/reader
- rdtsc 14y agoYou are right. My bad, I should just looked it up. Thought I could wing it from memory.
- pnathan 14y agoThat's an nice idea, using #<datastructure> as a straight-up constructor. I've always found hash-tables to be a colossal pain to create in Common Lisp. I tried to create a special JSON-esque syntax in CL for them, but it wasn't a smooth semantic drop-in.
- rdtsc 14y agoYeah I think it is pretty neat too. I had a couple of design ideas. This way I can persist some other structures (from Python for ex): For set(): (#set v1 v2 ...) For circular object graphs, I can encode an object reference to avoid serializing the same object twice: (#ref oid) Then of course there are custom class names I can pass in to serializer and deserializer: (#MyClass (attr1 val1) (attr2 val2) ...) This might not map to all languages but it worked for me.
- pnathan 14y agoCL uses # as a special character; I rewrote the idea into the (CL-standard idiom) `make-map` nomenclature: https://gist.github.com/4709844 https://gist.github.com/4709844 But yea, I hadn't thought of particularly using this idea. It's a good idea from Python (or maybe its older than that, IDK). :-)
- aerique 14y agoIsn't calling it a "colossal pain" a bit of hyperbole? A simple wrapper function should take away most of the pain.