Funs names in Erlang 17

后端 未结 3 1376
广开言路
广开言路 2021-01-13 00:32

Erlang 17 was released. And according to Erlang OTP 17.0 has been released:

Funs can now be given names

No examples are given.

相关标签:
3条回答
  • 2021-01-13 00:35

    Named funs are a implementation of EEP37 see the link for a detailed description and rationale.

    0 讨论(0)
  • 2021-01-13 00:54

    Joe Armstrong explains it in his blog post with an example.

    1> F = fun F(0) -> 1; 
               F(N) -> N * F(N - 1) 
           end.
    #Fun
    

    Previously you have to pass in the function as one of the args for anonymous recursive calls. (Think of y-combinator).

    1> F = fun(F, 0) -> 1;
              (F, N) -> N*F(F, N-1)
           end.
    #Fun
    
    0 讨论(0)
  • 2021-01-13 00:58

    This version doesn't need to pass in the function as one of the args:

    1> Fac = fun(Num) -> Foo = fun(F, 0) -> 1; (F, N) when N > 0, is_integer(N) -> N * F(F, N -1) end, Foo(Foo, Num) end.

    0 讨论(0)
提交回复
热议问题