Recursion of internal functions in Erlang

Playing with Erlang, I have a loop loop function:

process_loop(...A long list of parameters here...) ->
    receive
        ...Message processing logic involving the function parameters...
    end,
    process_loop(...Same long list of parameters...)
end.

It looks pretty ugly, so I tried this refactoring:

process_loop(...A long list of parameters...) ->
    Loop = fun() ->
        receive
            ...Message processing logic...
        end,
        Loop()
    end,
    Loop()
end.

But this turned out to be wrong, since the Loop variable is not connected inside the Loop function. So, I developed a workaround:

process_loop(...A long list of parameters...) ->
    Loop = fun(Next) ->
        receive
            ...Message processing logic...
        end,
        Next(Next)
    end,
    Loop(Loop)
end.

I have two questions:

  • Is there any way to implement the idea of โ€‹โ€‹fragment # 2, but without such โ€œsubsequent (subsequent)โ€ workarounds?

  • Do fragments No. 1 and No. 3 differ in terms of performance, or are they equivalent?

+3
source share
4 answers
  • No. Unfortunately, an anonymous function is simple. Anonymous unless you give them a name.

  • โ„–3 . , , . . - .

+2
  • / , . , .

  • ( ), . , , .

+2

# 1 , , . . , .

0

, , , , - , . . , . , .

, , , , .

"" , .

0

Source: https://habr.com/ru/post/1757606/


All Articles