Erlang 文档中的“escript”中有阶乘函数的简单实现。阶乘函数如下:
fac(0) -> 1;
fac(N) -> N * fac(N-1).
Run Code Online (Sandbox Code Playgroud)
没关系,我可以让它工作,没问题。
然而,我想知道如何使用“OTP 方式”实现相同的简单阶乘函数rebar3
?
需要明确的是,我的问题是:
仅供参考,我已经开始使用rebar3
. 这是我所在的位置:
rebar3 new app factorial
Run Code Online (Sandbox Code Playgroud)
创建了一些文件,但具体来说,代码位于src
目录中的 3 个文件中。我可以看到正在使用主管,看起来不错。
我可以从 shell 与该项目交互:
$ rebar3 shell
1> application:which_applications().
[{factorial,"An OTP application","0.1.0"},
{inets,"INETS CXC 138 49","7.0.3"},
{ssl,"Erlang/OTP SSL application","9.1.1"},
{public_key,"Public key infrastructure","1.6.4"},
{asn1,"The Erlang ASN1 compiler version 5.0.8","5.0.8"},
{crypto,"CRYPTO","4.4"},
{stdlib,"ERTS CXC 138 10","3.7"},
{kernel,"ERTS CXC 138 10","6.2"}]
2> application:stop(factorial).
=INFO REPORT==== 21-Jan-2019::12:42:07.484244 ===
application: factorial
exited: stopped
type: temporary
ok
3> application:start(factorial).
ok
Run Code Online (Sandbox Code Playgroud)
要“以 OTP 方式调用代码”,您可以将其放在gen_server
.
对于这个简单的阶乘函数,我factorial.erl
在src
目录中添加了一个新文件,该文件几乎是一个标准gen_server
框架,其中我的阶乘函数作为回调之一:
% factorial.erl
-module(factorial).
-behaviour(gen_server).
-export([start_link/0, stop/0, calc/1]).
<boilerplate gen_server stuff here, like init, etc.>
calc(N) ->
{ok, Result} = gen_server:call(?SERVER, {calc, N}),
{ok, Result}.
handle_call({calc, N}, _From, State) ->
Factorial = factorial(N),
Reply = {ok, Factorial},
{reply, Reply, State};
factorial(0) ->
1;
factorial(N) ->
N * factorial(N-1).
Run Code Online (Sandbox Code Playgroud)
自从我rebar3 new app factorial
创建了一个主管以来,我修改了主管,init
以便它调用我的factorial
模块:
% factorial_sup.erl
<skeleton supervisor stuff here>
init([]) ->
Server = {factorial, {factorial, start_link, []},
permanent, 2000, worker, [factorial]},
Children = [Server],
RestartStrategy = {one_for_one, 0, 1},
{ok, {RestartStrategy, Children}}.
Run Code Online (Sandbox Code Playgroud)
$ rebar3 shell
<Enter>
1> factorial:calc(5).
{ok,120}
Run Code Online (Sandbox Code Playgroud)
由于它是在主管下运行的,我们仍然可以停止并重新启动它:
2> application:stop(factorial).
=INFO REPORT==== 22-Jan-2019::13:31:29.243520 ===
application: factorial
exited: stopped
type: temporary
ok
3> factorial:calc(5).
** exception exit: {noproc,{gen_server,call,[factorial,{calc,5}]}}
in function gen_server:call/2 (gen_server.erl, line 215)
in call from factorial:calc/1 (/Users/robert/git/factorial/src/factorial.erl, line 32)
4> application:start(factorial).
ok
5> factorial:calc(5).
{ok,120}
Run Code Online (Sandbox Code Playgroud)
工作正在进行中:-)
。