[erlang-questions] Erlang Syntax - again

Hynek Vychodil vychodil.hynek@REDACTED
Tue Mar 11 13:27:37 CET 2008

On Tue, Mar 11, 2008 at 12:05 PM, Christian S <chsu79@REDACTED> wrote:

> > It is misunderstand what record is. Record is not this sort of things.
> Use
> > our own structure for this purpose. Record is not object, hash,
> dictionary
> > or whatever you really want. Record is not designed for some sort of
> > inheritance. Record is structure for memory friendly storage of fixed
> > "records" a which one will be changed very rare and just only by design.
> That is what they are.  What we want might be something else though.
> Thinking out loud:
> Would it be possible to create a pattern matching syntax for some kind
> of association map?
> As I understand it, guards without near O(1) properties are not that
> popular. But others do exist already.
> Perhaps something that recognizes {[Fields::atom()],
> FieldValue1::term()} so that
> both {[gurka, avocado, sallad, dressing], 1, 2, 3, 4} and {[tacos,
> salsa, avocado], 8, 4, 2} could be
> matched when asking for avocado=2 in this something.
> %% What better character to use for a beta feature than ß !!?
> cook(ß{avocado=1}) ->
>   guacamole().

You can wrote [{gurka,1}, {avocado,2}, {sallad,3}, {dressing,4}] and
{salsa, 4}, {avocado, 2}] and

cook(L) ->
    case proplist:get_value(avocado, L) of
         2 -> guacamole()

Your ß guard is not O(1) like mine solution. But again, it is not records
use case. You can make our own solution and erlange give you enough power to
do it and do it elegant. At least you can make your own parse transform to
do it with syntax sugar.
If {[gurka, avocado, sallad, dressing], 1, 2, 3, 4} and {[tacos,
salsa, avocado], 8, 4, 2} are #a{gurka= 1, avocado=2, salad=3, dressing=4}
and #b{tacos=8, salas=4, avocado=2} you can simply wrote

cook(#a{avocado=2}) ->
cook(#b{avocado=2}) ->

I know, this solution is only for finite number of record types and there is
nice solution for huge amount of records, look for records helper function (
8.7 in http://erlang.org/doc/reference_manual/records.html). And again, you
are looking for some other construct than record.

> The real wart in record _syntax_ is when you update a record in a
> record ( in a record ( in a record ...)).

--Hynek (Pichi) Vychodil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080311/55beb03e/attachment.htm>

More information about the erlang-questions mailing list