[erlang-questions] how to create user-defined guards

Jachym Holecek freza@REDACTED
Mon Apr 27 13:17:39 CEST 2009


# Ivan Carmenates Garcia 2009-04-27:
> get_students()->
>         F = fun()->
>                 mnesia:select(person_table, [{#person_table{
>                         id          ='$1',
>                         name     ='$2',
>                         group    ='$3',
>                         type      ='$4',
>                         id_card ='$5',
>                         user      ='$6'},
>                         [{'=:=', {is_student,'$4'}, true}],
>                         ['$_']}])
>         end,
>         mnesia:transaction(F).
> 
> is_student(X)->
>         if X == "Estudiante"->
>                 true;
>         true->
>                 false
>         end.
>
> [...]
> 
> How can I simulate or make my own guard or do something else to resolve this problem.

You can use arbitrary expressions in QLC queries; you could use the
following as transaction function (completely untested):

  F = fun () ->
              qlc:eval(qlc:q([Item || Item <- mnesia:table(person_table),
		is_person(Item)]))
      end

where is_person/1 is any Erlang function evaluating to boolean().

I don't know about QLC performance though and sure enough in a
simple case like yours you can just inline the is_student/1 test
into the matchspec and be done with it.

HTH,
	-- Jachym

PS: QLC needs -include_lib("stdlib/include/qlc.hrl").



More information about the erlang-questions mailing list