Erlang/OTP Forums

Author Message

<  User Contributions  ~  mapping_form.erl

tonyrog
Posted: Sun Sep 13, 2009 2:32 pm Reply with quote
User Joined: 12 Sep 2009 Posts: 43
mapping_form is a parse_transform that is used to construct constant mapping and reverse mapping tables.

The syntax is a bit like php arrays, the syntax accepted is:

mapping ( key1 = value1, key2 = value2 ... [ _ = default-value ] )

Where keys and values must be erlang terms.
The mapping construct returns a function that currently has three interface calls

Code:

M = mapping( a=1, b=2, c=3, d=3 ).
M(value, a).   % returns 1
M(key, 3).      % returns [c,d]
M(map,fun(K,V) -> {K,V} end).  %% returns [{a,1},{b,2},{c,3},{d,3}]


Please send suggestions about improvements to me.
The code is totally free to use in what ever way you want. As long as you
do not blame me.



mapping_form.erl
 Description:

Download
 Filename:  mapping_form.erl
 Filesize:  18.48 KB
 Downloaded:  895 Time(s)

View user's profile Send private message
tonyrog
Posted: Wed Sep 16, 2009 11:32 am Reply with quote
User Joined: 12 Sep 2009 Posts: 43
This update fixes issue with constant expression in keys and values. This makes mapping_form flexible when using it for defined constants using constant expressions. This update also adds optional type of mapping including:
one_to_one, one_to_many (default), many_to_one and many_to_many.


Example:
Code:

-define(A, (16#80 bor 1)).
-define(B, (16#80 bor 2)).

mapping (one_to_one,  ?A = a, ?B = b ).


Syntax:
mapping ([Type,] K1=V1, .. Kn=Kv [, _=Vd ])

Where
optional Type is:
one_to_one: Both keys and values must be unique
Ki -> Vi, Vi -> Ki
one_to_many: Keys must be unique
Ki -> Vi, Vi -> [Ki,Kj..] (default)
many_to_one: Values must be unique
Ki -> [Vi,Vj,..], Vi -> Ki
many_to_many:
Ki -> [Vi,Vj,..], Vi -> [Ki,Kj,...]

The generated mapping is a function that accepts two arguments:
Mapping(value, Ki) -> Vi | [Vi,Vj..]
Mapping(key, Vi) - > Ki | [Ki,Kj..]
Mapping(map, fun(K,V) -> .. end) -> [f(K1,V1)...f(Kn,Vn)]

Ki MUST be a constant OR constant expression
Vi MUST be a constant OR constant expression OR
a mapping (reverse mapping => [])

The default mapping can only be used in
the one_to_many (default) and many_to_many mappings
The reverse mapping for the default case is not defined,
and is only used for convenience.
The reverse mapping of a mapping is not defined (yet) and
is handled as the empty list.



mapping_form.erl
 Description:

Download
 Filename:  mapping_form.erl
 Filesize:  22.92 KB
 Downloaded:  601 Time(s)

View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum