bizarre match specs
Ulf Wiger (AL/EAB)
ulf.wiger@REDACTED
Thu Jan 19 17:17:16 CET 2006
Oh, I apparently forgot to mention that I wanted
to emulate the pattern "foo*.erl" using a match spec.
The emulation currently only supports up to two
characters matching the wildcard part, which was
exactly what was needed for my test set. Since the
pattern is recursive, we need only extend it further,
until something breaks.
/Uffe
> -----Original Message-----
> From: owner-erlang-questions@REDACTED
> [mailto:owner-erlang-questions@REDACTED] On Behalf Of Ulf
> Wiger (AL/EAB)
> Sent: den 19 januari 2006 16:57
> To: Erlang Questions
> Subject: bizarre match specs
>
>
> 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',".e
> rl"},{'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