Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Functional programming 仅从erlang中的引用中提取数字_Functional Programming_Erlang - Fatal编程技术网

Functional programming 仅从erlang中的引用中提取数字

Functional programming 仅从erlang中的引用中提取数字,functional-programming,erlang,Functional Programming,Erlang,我是新来的二郎。 我只需要取make_ref()返回的数字。 因此,如果make_ref()返回:#ref 我想从中提取441 你知道怎么做吗?试试这个: unique_integer() -> try erlang:unique_integer() catch error:undef -> {MS, S, US} = erlang:now(), (MS*1000000+S)*100000

我是新来的二郎。 我只需要取make_ref()返回的数字。 因此,如果make_ref()返回:#ref 我想从中提取441

你知道怎么做吗?

试试这个:

unique_integer() ->
    try
        erlang:unique_integer()
    catch
        error:undef ->
            {MS, S, US} = erlang:now(),
            (MS*1000000+S)*1000000+US
    end.
编辑
此解决方案与使用
io_lib:format(“~p”,[Ref])
提取整数的主要区别在于速度。当我的解决方案在R18中大约需要40ns时,转换为列表、regexp和返回到整数需要9µs。我会选择两个数量级更快的解决方案。

erlang:ref_to_list/1
函数将ref转换为列表,但文档警告不要在生产代码中使用它。我们可以改用
io\u lib:format/2
。将ref转换为列表后,我们可以使用regexp提取数字

下面是一个函数,用于从ref的字符串表示形式中提取数字:

extract_from_ref(Ref) when is_reference(Ref) ->
    [RefString] = io_lib:format("~p", [Ref]),
    {match, L} = re:run(RefString,
                        "#Ref<(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)>",
                        [{capture, all_but_first, list}]),
    IntList = lists:map(fun list_to_integer/1, L),
    list_to_tuple(IntList).

你为什么需要这个?我需要创建一个uniqe编号,ref在其中。不能保证ref在其中有唯一的编号。ref保证是唯一的,但数字是ref的字符串表示形式。这绝对不是生成唯一数字的正确方法。还请注意,现代Erlang运行时有
Erlang:unique\u integer
函数,它正是这样做的
{_, _, _, N} = extract_ref(make_ref()).