Erlang中的一流模式?(备选方案)

Erlang中的一流模式?(备选方案),erlang,pattern-matching,elixir,first-class,Erlang,Pattern Matching,Elixir,First Class,有没有办法在Erlang中创建第一类模式?我需要能够创建模式并将其作为参数传递给其他函数,但我知道模式不是Erlang中的第一类。我也看了长生不老药,但它似乎并没有提供更多的模式去 我想知道是否有人想出了一个简单的解决这个问题的办法。我在考虑尝试实现如下内容: % Instead of using variables, we would just use uppercase atoms which would serve as vars % A passable pattern Pattern

有没有办法在Erlang中创建第一类模式?我需要能够创建模式并将其作为参数传递给其他函数,但我知道模式不是Erlang中的第一类。我也看了长生不老药,但它似乎并没有提供更多的模式去

我想知道是否有人想出了一个简单的解决这个问题的办法。我在考虑尝试实现如下内容:

% Instead of using variables, we would just use uppercase atoms which would serve as vars
% A passable pattern
Pattern = {ok, 'Result'}. 

% Custom function to check for matches
match(pattern, {ok, [1,2,3]}). % => true
-module (match).

-compile([export_all]).

-define(MF(S), fun(S) -> true; (_)->false end).


match(F,V) -> F(V).


test() ->
    Pattern = ?MF({ok,_}),
    false = match(Pattern,{error,reason}),
    true = match(Pattern,{ok,[1,2,3]}).
我是新来的二郎,所以这可能是完全没有必要的。也许有一个图书馆做这种事


非常感谢您的建议。提前谢谢

我不确定我是否看到了您的整个问题,但谓词函数似乎很适合您。在函数式语言中使用泛型函数参数化泛型函数是非常常见的方法。看看列表函数,比如map、foldl、filter。

您可能想看看,我相信这就是您所询问的模式类型。它们用于在中以及中匹配值。你可能会在那里找到一些灵感。

我不知道是否已经存在满足你需要的东西,但你可以像这样轻松地实现它:

% Instead of using variables, we would just use uppercase atoms which would serve as vars
% A passable pattern
Pattern = {ok, 'Result'}. 

% Custom function to check for matches
match(pattern, {ok, [1,2,3]}). % => true
-module (match).

-compile([export_all]).

-define(MF(S), fun(S) -> true; (_)->false end).


match(F,V) -> F(V).


test() ->
    Pattern = ?MF({ok,_}),
    false = match(Pattern,{error,reason}),
    true = match(Pattern,{ok,[1,2,3]}).

我最终使用Elixir的宏功能实现了类似于Erlang的match Spec的东西。代码看起来更简洁(因为我只是用模式定义函数),它们的工作方式与Erlang的match Spec非常相似。

match specification的一个问题是没有可用的函数来检查match specification是否对特定输入有效。你必须自己处理它们,而谓词非常容易使用,并且符合函数式编程的本质。
ets:select
中使用的模式看起来很有趣。是否有用于使用匹配规格的库?