what is proptuple? #2

Open
benbro opened this Issue Aug 2, 2010 · 7 comments

3 participants

@benbro

Can you explain what is a proptuple?
In the erlang shell I'm getting an exception when I try:
P = [{<<"a">>, <<"b">>}, {<<"c">>, <<"d">>}].
list_to_atom(P).

"proptuple is result of passing proplist to list_to_atom bif"

Thanks

@noelbush

I think he meant list_to_tuple.

@benbro

so [{<<"a">>, <<"b">>}, {<<"c">>, <<"d">>}] becomes {{<<"a">>, <<"b">>}, {<<"c">>, <<"d">>}}. I still don't see how it solve the need for tagging.

@lambder
Owner

The "proptuple" is a term I have coined. It refers to well known term "proplist". Proplists are not a special erlang's type. Proplists are ordinary lists containing entries in the form of tuples, whose first elements are keys used for lookup and insert on it. (source: http://www.erlang.org/doc/man/proplists.html ). Proptuples are similar but instead of having list of 2-tuples we have tuple of 2-tuples. The reason for its existence is to have corresponding type to JSON's hash type. So jsonerl maps erlang's list to JSON's arrays (and vice versa) and erlang's "proptuples" to JSON's hashes (and vice versa). Having that we don't need to tag our objects (which in JS/JSON universe look silly).

@benbro

Is the fact that list_to_atom bif turns a proplist to proptuple is a hack that might be removed in a future erlang version?

Erlang has useful functions in the proplist module. How do you work with proptuples?
How do you turn a proptuple back to a proplist?

Tagging hash in erlang is ugly and I like what you are trying to do but is it useful in real world scenario? Isn't going back and forth between proplist and proptuple harder than just using {struct, [proplsit]} ?

@lambder
Owner

Proptuple is not about being easier to use than to user proplists. In fact they are very similar. Simply instead of list of 2-tuples, proptuple is just tuple of 2-tupes. The reason for proptuple is to have closer compatibility to json. And as someone on #couchdb pointed out, mochiweb json library is already avoiding tagging erlang tuples.

@benbro

A tuple is an efficient data type that can't be changed while you can add and remove items from a list. That's why I asked if you have good experience with proptuples in real life.

Can you point me to the mochiweb json code that avoids tagging?
From what I see it still use {struct, [proplist]}
https://github.com/mochi/mochiweb/blob/master/src/mochijson2.erl

@lambder
Owner

In erlang you can't change list nor tuple. Eralng data-structures are persistent and immutable. Every modification creates new version of the structure often reusing the previous one eliminating the need for doing full copy.
I havent benchmarked performance of updates list versus tuples. All depends on what place are you going to modify. It's very cheap to modify (change, remove, add) head of the list but it is not the case for elements close to its end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment