[erlang-questions] Converting list of tagged tuples to a record

Bengt Kleberg bengt.kleberg@REDACTED
Thu Sep 2 08:48:52 CEST 2010


Greetings,

After 10 years of programming Erlang I still use the method described
below (1) when moving data from a user interface.
The same goes for using records in records. It is rather awkward to
update the innermost records, so it helps to use functions. 


bengt

On Thu, 2010-09-02 at 08:23 +0200, Silas Silva wrote:
> Hello all,
> 
> In my newbie application, I receive a bunch of data from HTTP's POST
> (I'm using Mochiweb), in the following format (supposing a person's
> data):
> 
>     [{first_name, value}, {last_name, value}, ...]
> 
> And I have the definition of the record for the person:
> 
>     -record(person, {first_name, last_name, ...}).
> 
> alistair in the #erlang channel in freenode, gave me a nice solution to
> put this list in the record:
> 
>     lists:foldl(fun add_to_record/2, #person{}, List).
> 
>     add_to_record({first_name, value}, rec) ->
>         rec#person{first_name = value};
> 
>     add_to_record({last_name, value}, rec) ->
>         rec#person{last_name = value};
> 
>     ...
> 
> That works.  My questions are:
> 
> 1. Are there other [good practice] ways to transform information coming
>    from a UI (which, must of the times, come as a list) in well
>    specified records?
> 
> 2. Is it common to use records of records?  Is it possible?  For example:
> 
>     -record(name, {last_name, first_name}).
>     -record(person, name, ...)
> 
> Thanks!
> 



More information about the erlang-questions mailing list