erlang使用Tips

来源:互联网 发布:淘宝直播小陈珍珠 编辑:程序博客网 时间:2024/06/04 23:27

.user_default模块

你可以将常用的函数放入user_default模块,这样在使用时,就不用输入模块名, 很方便!


%cat ~/.erlang

%file:set_cwd("~/money"). %%donot use this line

code:load_abs("~/money/user_default").

code:add_pathz("/media/G/RnD/erlang/cean-1.4/ebin").


%cat ~/money/user_default.erl

-module(user_default).
-author('sw2wolf@163.com').
-compile(export_all).

-import(calendar, [date_to_gregorian_days/3, gregorian_days_to_date/1, day_of_the_week/1]).

-ifdef(Debug).
-define(DEBUG(Fmt, Args), io:format(Fmt, Args)). 
-else.
-define(DEBUG(Fmt, Args), no_debug). 
-endif. 

%---------------------------------------------------------------------
%       stock
%---------------------------------------------------------------------

-define(SXF, 0.0015). %手续费
-define(YHS, 0.001).  %印花费
-define(GHF, 1.0).    %过户费
%计算股票盈利
winG(Qty,Pb,Ps) -> Qty * Ps * (1 -?SXF - ?YHS) - 2 * ?GHF - Qty * Pb * (1 + ?SXF).

%算权证盈利
winQ(Qty,Pb,Ps) ->
    Qty * Ps * (1 - ?SXF) - 2 * ?GHF - Qty * Pb * (1 + ?SXF).

-define(NO_TRADE_DAYS,[{2009,1,1},
                       {2009,5,1},{2009,5,2}, {2009,5,3},{2009,5,28},
                       {2009,10,1}, {2009,10,2}, {2009,10,3},{2009,10,4},{2009,10,5},{2009,10,6},{2009,10,7},{2009,10,8}
                      ]).
-define(RATIO,[0.119, 0.236, 0.382, 0.5, 0.618, 0.809]).


%
%从所给日期起经过N个交易日后是哪天?
%
date_by_ntday({Y,M,D}, NTday) when NTday > 0 ->
    do_date_by_ntday(next_day({Y,M,D}), NTday).

do_date_by_ntday({Y,M,D}, 0) -> {Y,M,D};
do_date_by_ntday({Y,M,D}, NTday) ->
    case is_trade_day({Y,M,D}) of
        true ->
            if NTday == 1 -> do_date_by_ntday({Y,M,D}, NTday-1);
                true -> do_date_by_ntday(next_day({Y,M,D}), NTday-1)
            end;
        _    -> do_date_by_ntday(next_day({Y,M,D}), NTday)
    end.

%对所给价格段作黄金分割
div618_P(P1, P2) ->
    F = fun(R) ->
        io:format("---~.3f  ~.3f---\n",[R, P1 + (P2-P1)*R])
    end,
    lists:foreach(F,lists:reverse(?RATIO)) .

%对所给时间段作黄金分割
div618_T({Y1,M1,D1}, {Y2,M2,D2}) ->
    Days = trade_days({Y1,M1,D1}, {Y2,M2,D2}),
    NDay = length(Days),
    io:format("The number of trading days=~p~n", [NDay]),
    F = fun(R) ->
        Low = trunc(1 + (NDay - 1) * R),
        High = round(1 + (NDay - 1) * R + 0.5),

        io:format("~p~n", [lists:nth(Low, Days)]),
        io:format("----------~p---------~n",[R]),
        io:format("~p~n~n", [lists:nth(High, Days)])
    end,
    lists:foreach(F, ?RATIO) .
    
trade_days({Y1,M1,D1}, {Y2,M2,D2}) ->
    Day1 = date_to_gregorian_days(Y1, M1, D1+1),
    Day2 = date_to_gregorian_days(Y2, M2, D2),
    F = fun(E, Acc) ->
        {Y, M, D} = gregorian_days_to_date(E),
        case is_trade_day({Y, M, D}) of
            true -> [{Y, M, D} | Acc] ;
            _ -> Acc
        end
    end,
    Days = lists:reverse(lists:foldl(F, [],lists:seq(Day1, Day2)) ),
    Days.

%判断所给日期是交易日吗?
is_trade_day({Y,M,D}) ->
    DOW = day_of_the_week({Y,M,D}),
    if DOW == 6 orelse DOW == 7 -> false;
    true ->
        case lists:member({Y,M,D}, ?NO_TRADE_DAYS) of
            true -> false;
            _ -> true
        end
    end.

next_day({Y, M, D}) ->
    Day1 = date_to_gregorian_days(Y, M, D),
   gregorian_days_to_date(Day1 + 1).

%运行系统命令

run(Cmd) -> 
    Res = string:tokens(os:cmd(Cmd),"\n"),
    lists:foreach(
        fun(X)->io:format("~ts~n",[unicode:characters_to_list (erlang:iolist_to_binary(X))]) end,Res).


%erl
Erlang R13B (erts-5.7.1) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.1  (abort with ^G)

1> self(). 
<0.31.0>
2> catch_exception(true).   %%change this behavior of the shell
false
3> 1 = 2.
* exception error: no match of right hand side value 2
4> self().
<0.31.0>


5> winG(1000,4.48,4.53).            %% 不用输入模块名
29.954999999999927


.创建定制的behavior

To create your own behavior, you must make a module that exports a function,behaviour_info/1. Note that while Erlang is American-English-friendly in regards to declaring behaviors, it is not as friendly when you are defining them. For example, you can use either -behavior(gen_server). or -behaviour(gen_server). when you declare you module implements the gen_server behavior, but if you name the behaviour_info/1 function without the 'u' it will not work.

behaviour_info/1 just needs to return the list of required exports of an implementation. Here's an example.


-module(gen_foo).
-export([behaviour_info/1]).

behaviour_info(callbacks) ->
   [{foo, 0}, {bar, 1}, {baz, 2}];
behavior_info(_) ->
   undefined.

This declares three callbacks for the gen_foo behavior: foo/0, bar/1, and baz/2. behaviour_info/1 simply returns an array of 2-tuples with the function name and arity.

When a module declares that it implements gen_foo now, the Erlang compiler will check to make sure it exports and implements the required callbacks, and it will print warnings if this is not the case.


.erlang dump的查看方法

在erl中执行webtool:start(), 然后使用浏览器访问localhost:8888端口。



原创粉丝点击