erlang - 以编程方式使用模式匹配

标签 erlang

是否可以通过编程方式进行模式匹配?

Pattern = {error, '_'},
IsError = 
    case {error, "foo"} of
        Pattern -> true;
        _ -> false
    end.

我知道我可以使用宏来做到这一点,但我有一个我想要匹配的动态模式列表,这些模式是提前未知的。

最佳答案

也许您能得到的最接近的是使用编译的 match specification ,通过调用函数 ets:match_spec_compileets:match_spec_run:

MS = ets:match_spec_compile([{{error, '_'}, [], ['$_']}]).
Items = [ok, {error, foo}, {error, bar}].
ets:match_spec_run(Items, MS).

这将返回 Items 列表中匹配的两个项目:

[{error,foo},{error,bar}]

关于erlang - 以编程方式使用模式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72913706/

相关文章:

Erlang Rebar escriptize 和 nifs

erlang - 如何加入位串列表?

debugging - 检查 Erlang 故障转储 - 如何计算所有内存?

erlang - -spec, -type, -opaque 在 erlang 编程中是什么意思

erlang - 大文件的写入会阻止 Erlang VM 吗?

compiler-errors - Erlang编译错误 “No command to run specified!”

matrix - 如何在 Erlang 中将列表的列表相乘?

erlang - net_kernel :start fails with `{' EXIT', nodistribution}`

algorithm - erlang中的分布式算法模拟

Erlang 脚本参数