bizarre match specs

Ulf Wiger (AL/EAB) ulf.wiger@REDACTED
Thu Jan 19 16:56:33 CET 2006


I was toying around with using match specifications
to search on filenames. The idea was basically to 
take a match specification and compile parts of it to
a regexp-like thingie, and traverse a file tree.

(There is a reason, which at least in my mind seems 
sound, but that's a long story.)

Example:

1> MS = fun(Objs,Ms) -> Bin=ets:match_spec_compile(Ms),
             ets:match_spec_run(Objs, Bin) end.

2> L=[{"foo_a.erl",a},{"foo.erl",b},{"fooerl",c}].
[{"foo_a.erl",a},{"foo.erl",b},{"fooerl",c}]

3> MS(L,[{{"foo" ++
'$1','_'},[{'andalso',{'=/=','$1',[]},{'orelse',{'==','$1',".erl"},{'ore
lse',{'andalso',{'=/=',{'tl','$1'},[]},{'orelse',{'==',{tl,'$1'},".erl"}
,{'andalso',{'=/=',{'tl',{'tl','$1'}},[]},{'==',{'tl',{'tl','$1'}},".erl
"}}}}}}}],['$_']}]).
[{"foo_a.erl",a},{"foo.erl",b}]

Cool, eh? /:

Obviously, it's too ground to be generally useful, and 
I haven't tested how deeply I can nest the guards before
the whole thing chokes or gets totally useless.

Boy, would I like to have a regexp pattern in the match spec,
or perhaps 'let' and 'skipwhile' in the guards... ;-)

{{"foo" ++ '$1', '_'}, 
 [{'andalso',{'=/=','$1',[]},
   {let, '$2',
    {skipwhile, {tl,'$1'},
     {'=/=',{'hd','$2'},$\n}
     {'==', '$2', ".erl"}}}}],
 ['$_']}

One could also imagine bit syntax expressions in 
match specifications, right?  (:

Or should I just look for thrills elsewhere?

/Uffe



More information about the erlang-questions mailing list