Pattern matching: Skipping the rest of a tuple
Ulf Wiger
ulf@REDACTED
Wed Dec 28 23:27:16 CET 2005
Well, if you want to do something really absurd, try this:
Eshell V5.4.8 (abort with ^G)
1> rd(rec, {a,b,c}).
rec
2> R = #rec{a=1,b=2,c=3}.
#rec{a = 1,b = 2,c = 3}
3> MS = ets:match_spec_compile([{#rec{a=1,_='_'},[],[true]}]).
<<>>
4> ets:match_spec_run([R],MS).
[true]
13> MS2 =
ets:match_spec_compile([{{rec1,1,'_'},[],[true]},{{rec2,1,'_','_'},[],
[true]}]).
<<>>
14> ets:match_spec_run([{rec1,1,2},{rec2,1,1,2}],MS2).
[true,true]
Actually executing match_spec_run/2 is most likely quite
efficient as well. (:
/Uffe
Den 2005-12-28 20:19:54 skrev Joel Reymont <joelr1@REDACTED>:
> Folks,
>
> Is it possible to write a function to pattern-match on tuples of various
> sizes _without_ using element(X, Tuple)?
>
> I'm dealing with records of different length and would like to say
> things like
>
> match({record1, _, 20, _/rest}) -> ...
> match({record2, 30, _/rest}) -> ...
> etc.
>
> I don't believe there is a syntax for "I don't care about the rest of
> the tuple". It looks to me like I would need to supply _ for every
> element that I'm ignoring.
>
> Is that right?
>
> I know about { ..., _ = _ } but I think it only works on assignment.
>
> Thanks, Joel
>
> --
> http://wagerlabs.com/
>
>
>
>
>
--
Ulf Wiger
More information about the erlang-questions
mailing list