将数字列表分成偶数,奇数两个列表
%%%-------------------------------------------------------------------
%%% @author ming
%%% @copyright (C) 2019, <COMPANY>
%%% @doc
%%% 将数字列表分成偶数,奇数两个列表
%%% @end
%%% Created : 26. 六月 2019 10:10
%%%-------------------------------------------------------------------
-module(lib_misc).
-author("ming").
%% API
-export([start/0,odds_and_evens_acc/3, odds_and_evens1/1, odds_and_evens2/1]).
%% 方法一:遍历两次列表
odds_and_evens1(L) ->
Odds = [X || X <- L, (X rem 2) =:= 1],
Evens = [X || X <-L, (X rem 2) =:= 0],
{Odds, Evens}.
%% 方法二:只需遍历一次列表
odds_and_evens2(L) ->
odds_and_evens_acc(L, [], []).
odds_and_evens_acc([H|T], Odds, Evens) ->
case (H rem 2) of
1 -> odds_and_evens_acc(T, [H|Odds], Evens);
0 -> odds_and_evens_acc(T, Odds, [H|Evens])
end;
odds_and_evens_acc([], Odds, Evens) ->
{Odds, Evens}.
start() ->
L = [1, 3, 4, 5, 6, 7, 9, 15, 16],
odds_and_evens2(L).
统计字符串行数
%%%-------------------------------------------------------------------
%%% @author ming
%%% @copyright (C) 2019, <COMPANY>
%%% @doc
%%% 统计字符串行数
%%% @end
%%% Created : 26. 六月 2019 11:21
%%%-------------------------------------------------------------------
-module(yinshe).
-author("ming").
%% API
-export([count_character/1, count_character/2, start/0]).
count_character(Str) ->
count_character(Str, #{ }).
%% 统计函数
%%count_character([H|T], #{ H := N }=X) -> %%貌似因为版本问题,这种写法编译出错
%% count_character(T, X#{ H := N+1 });
count_character([H|T], X) ->
case maps:find(H, X) of
{ok, N} ->
count_character(T, X#{ H := N+1 }); %%X#{ H := N+1 }更新映射组X里H的值+1
error ->
count_character(T, X#{ H => 1 })
end;
count_character([], X) ->
X.
start() ->
count_character("asdasdasdddz").
异常处理
%%%-------------------------------------------------------------------
%%% @author ming
%%% @copyright (C) 2019, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 26. 六月 2019 14:20
%%%-------------------------------------------------------------------
-module(try_test).
-author("ming").
%% API
-export([generate_exception/1, demo1/0, demo2/0, demo3/0, demo4/1, demo5/0]).
generate_exception(1) -> a;
generate_exception(2) -> throw(a);
generate_exception(3) -> exit(a);
generate_exception(4) -> {'Text', a};
generate_exception(5) -> error(a).
demo1() ->
[catcher(I) || I <- [1, 2, 3, 4, 5]].
%% 处理异常1
catcher(N) ->
try generate_exception(N) of
Val -> {N, normal, Val}
catch
throw:X -> {N, caught, thrown, X};
exit:X -> {N, caught, exited, X};
error:X -> {N, caught, error, X}
end.
%% 处理异常2(异常信息较详细)
demo2() ->
[{I, (catch generate_exception(I))} || I <- [1, 2, 3, 4, 5]].
%% 栈跟踪
demo3() ->
try generate_exception(5)
catch
error:X ->
{X, erlang:get_stacktrace()}
end.
%% 处理读取文件异常
demo4(File) ->
try file:read_file(File) of
{ok, Val} -> Val
catch
throw:X -> X
end.
%% 捕捉一切可能异常
demo5() ->
try generate_exception(5)
catch
_:_ -> _
end.
二进制型
aaa() ->
B = <<"2s5A链接发来就"/utf8>>, %%utf8二进制型 utf8一个汉字占3位 <<50,115,53,65,233,147,190,230,142,165,229,143,145,230,157,165,229,176,177>>
<<35481:32/little>>, %%<<153,138,0,0>>
<<35481:32/big>>, %%<<0,0,138,153>>
case B of
<<A1:8, A2:8, A3:8, A4: 8, A5:8, _/binary>> -> %% bit
{A1, A2, A5}; %% {50, 115, 233}
<<A1:1/binary, Rest/binary>> -> %% byte
{A1, Rest}
end.