erlang各种有用的函数包括一些有用nif封装,还有一些性能测试case。
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

271 行
7.8 KiB

5 个月前
5 个月前
5 个月前
5 个月前
5 个月前
5 个月前
5 个月前
5 个月前
5 个月前
5 个月前
  1. -module(utList).
  2. -define(TRUE, true).
  3. -define(FALSE, false).
  4. -define(BREAK, break).
  5. -define(BREAK(Value), {?BREAK, Value}).
  6. -define(CONTINUE, continue).
  7. -define(CONTINUE(Value), {?CONTINUE, Value}).
  8. -define(UNDEFINED, undefined).
  9. -compile([export_all, nowarn_export_all]).
  10. -spec getListVal(term(), list()) -> term().
  11. getListVal(Key, List) ->
  12. case lists:keyfind(Key, 1, List) of
  13. {_Key, Value} ->
  14. Value;
  15. _ ->
  16. undefined
  17. end.
  18. -spec getListVal(term(), list(), term()) -> term().
  19. getListVal(Key, List, Default) ->
  20. case lists:keyfind(Key, 1, List) of
  21. {_Key, Value} ->
  22. Value;
  23. _ ->
  24. Default
  25. end.
  26. while(Fun, CurData) ->
  27. case Fun(CurData) of
  28. ?BREAK -> CurData;
  29. ?BREAK(BreakData) -> BreakData;
  30. ?CONTINUE ->
  31. while(Fun, CurData);
  32. ?CONTINUE(Continue) ->
  33. while(Fun, Continue)
  34. end.
  35. %% For loop.
  36. for(N, Fun) ->
  37. for(1, N, 1, Fun).
  38. for(Start, End, Fun) ->
  39. for(Start, End, 1, Fun).
  40. for(End, End, _Step, Fun) ->
  41. Fun(End);
  42. for(Start, End, Step, Fun) ->
  43. Fun(Start),
  44. for(Start + Step, End, Step, Fun).
  45. %% 带返回状态的for循环 @return {ok, State}
  46. forWithState(Index, Max, _Fun, State) when Index > Max ->
  47. {ok, State};
  48. forWithState(Max, Max, Fun, State) ->
  49. Fun(Max, State);
  50. forWithState(Index, Max, Fun, State) ->
  51. {ok, NewState} = Fun(Index, State),
  52. forWithState(Index + 1, Max, Fun, NewState).
  53. forWhile(Fun, State) ->
  54. forWhile(?CONTINUE, Fun, State).
  55. forWhile(?BREAK, _Fun, State) ->
  56. State;
  57. forWhile(?CONTINUE, Fun, State) ->
  58. {Next, NewState} = Fun(State),
  59. forWhile(Next, Fun, NewState).
  60. %% 可以重复获取
  61. randFormList([]) ->
  62. false;
  63. randFormList(List) ->
  64. lists:nth(rand:uniform_s(1, length(List)), List).
  65. %% 不能重复获取
  66. randFormListOnce([]) ->
  67. {false, []};
  68. randFormListOnce(List) ->
  69. Nth = rand:uniform_s(1, length(List)),
  70. NthData = lists:nth(Nth, List),
  71. {NthData, lists:delete(NthData, List)}.
  72. randDelete(RemainList) ->
  73. Pos = rand:uniform_s(1, length(RemainList)),
  74. remove_nth(Pos, RemainList).
  75. randDelete(DelCount, List) ->
  76. FunDel =
  77. fun(_, RemainList) ->
  78. RemainList(RemainList)
  79. end,
  80. lists:foldl(FunDel, List, lists:seq(1, DelCount)).
  81. %%随机从集合中选出指定个数的元素length(List) >= Num
  82. %%[1,2,3,4,5,6,7,8,9]中选出三个不同的数字[1,2,4]
  83. getRandFromList(Num, List) ->
  84. ListSize = length(List),
  85. FunDel =
  86. fun(N, List1) ->
  87. Random = rand:uniform_s(1, (ListSize - N + 1)),
  88. Elem = lists:nth(Random, List1),
  89. lists:delete(Elem, List1)
  90. end,
  91. Result = lists:foldl(FunDel, List, lists:seq(1, Num)),
  92. List -- Result.
  93. %%打乱列表函数 List =[] 返回 打乱后列表 List2 元素小于300多个的时候 使用confuseList1 大于300多个的时候使用confuseList2
  94. %% ----------------------------------------------------
  95. confuseList1(List) ->
  96. TupleList = list_to_tuple(List),
  97. TupleSize = tuple_size(TupleList),
  98. confuseList1(TupleSize, TupleSize, TupleList).
  99. confuseList1(0, _TupleSize, TupleList) -> tuple_to_list(TupleList);
  100. confuseList1(Index, TupleSize, TupleList) ->
  101. ChangeIndex = rand:uniform(TupleSize),
  102. Value1 = element(Index, TupleList),
  103. Value2 = element(ChangeIndex, TupleList),
  104. TupleList1 = setelement(Index, TupleList, Value2),
  105. TupleList2 = setelement(ChangeIndex, TupleList1, Value1),
  106. confuseList1(Index - 1, TupleSize, TupleList2).
  107. confuseList2(List) ->
  108. Array = array:from_list(List),
  109. ArraySize = array:size(Array),
  110. confuseList2(ArraySize, ArraySize, Array).
  111. confuseList2(0, _TupleSize, TupleList) -> array:to_list(TupleList);
  112. confuseList2(Index, TupleSize, TupleList) ->
  113. ChangeIndex = rand:uniform(TupleSize),
  114. Value1 = array:get(Index, TupleList),
  115. Value2 = array:get(ChangeIndex, TupleList),
  116. TupleList1 = array:set(Index, Value2, TupleList),
  117. TupleList2 = array:set(ChangeIndex, Value1, TupleList1),
  118. confuseList2(Index - 1, TupleSize, TupleList2).
  119. transferMaps([], Index, Map) -> {Index - 1, Map};
  120. transferMaps([Element | List], Index, Map) ->
  121. transferMaps(List, Index + 1, Map#{Index => Element}).
  122. confuseList3(List) ->
  123. {Cnt, Map} = transferMaps(List, 1, #{}),
  124. confuseList3(Cnt, Cnt, Map).
  125. confuseList3(0, _Size, Map) -> maps:values(Map);
  126. confuseList3(Index, Size, Map) ->
  127. ChangeIndex = rand:uniform(Size),
  128. #{Index := Value1, ChangeIndex := Value2} = Map,
  129. confuseList3(Index - 1, Size, Map#{Index := Value2, ChangeIndex := Value1}).
  130. %%根数Tuple元素数量,把List转成Tuple 返回:{TupleList, RemainList}
  131. list_to_tuple(List, TupleCount) ->
  132. list_to_tuple(List, TupleCount, []).
  133. list_to_tuple(List, TupleCount, Result) when length(List) >= TupleCount ->
  134. {List1, List2} = lists:split(TupleCount, List),
  135. list_to_tuple(List2, TupleCount, [list_to_tuple(List1) | Result]);
  136. list_to_tuple(List, _TupleCount, Result) ->
  137. {lists:reverse(Result), List}.
  138. %%计算元素在列表中出现几次(5, [1,2,3,5,5,5,3]) ----> 3
  139. repeatCount(Element, List) ->
  140. repeatCount(Element, List, 0).
  141. repeatCount(Element, List, Count) ->
  142. case lists:member(Element, List) of
  143. false -> Count;
  144. true ->
  145. repeatCount(Element, lists:delete(Element, List), Count + 1)
  146. end.
  147. remove_nth(RemainList, Pos) ->
  148. Head = lists:sublist(RemainList, 1, Pos - 1),
  149. End = lists:sublist(RemainList, Pos + 1, length(RemainList)),
  150. Head ++ End.
  151. %%删除指定元素返回:(5, [1,2,4,4,5,5,4,5]) -> [1,2,4,4,4]
  152. remove_element(Element, List) ->
  153. case lists:member(Element, List) of
  154. false -> List;
  155. true -> remove_element(Element, lists:delete(Element, List))
  156. end.
  157. %%删除重复元素 [1,2,3,2,3,4] ---> [1,2,3,4]
  158. remove_repeat(List) ->
  159. remove_repeat(List, []).
  160. remove_repeat([], Result) ->
  161. Result;
  162. remove_repeat([L | List], Result) ->
  163. case lists:member(L, Result) of
  164. false -> remove_repeat(List, Result ++ [L]);
  165. true -> remove_repeat(List, Result)
  166. end.
  167. %%根据Key去查List中元素的Nth位相等的匹配到的元素Fun(Element)返回
  168. find_elements(Key, Nth, List, Fun) ->
  169. InnerFun = fun(Element) ->
  170. case element(Nth, Element) =:= Key of
  171. true ->
  172. {true, Fun(Element)};
  173. false ->
  174. false
  175. end
  176. end,
  177. lists:filtermap(InnerFun, List).
  178. %%对list进行去重,排序
  179. %%Replicat 0不去重,1去重
  180. %%Sort 0不排序,1排序
  181. filter_list(List, Replicat, Sort) ->
  182. if Replicat == 0 andalso Sort == 0 ->
  183. List;
  184. true ->
  185. if Replicat == 1 andalso Sort == 1 ->
  186. lists:usort(List);
  187. true ->
  188. if Sort == 1 ->
  189. lists:sort(List);
  190. true ->
  191. lists:reverse(filter_replicat(List, []))
  192. end
  193. end
  194. end.
  195. %%list去重
  196. filter_replicat([], List) ->
  197. List;
  198. filter_replicat([H | Rest], List) ->
  199. Bool = lists:member(H, List),
  200. List1 =
  201. if Bool == true ->
  202. [[] | List];
  203. true ->
  204. [H | List]
  205. end,
  206. List2 = lists:filter(fun(T) -> T =/= [] end, List1),
  207. filter_replicat(Rest, List2).
  208. %%交集
  209. get_intersection_list(A, B) when is_list(A) andalso is_list(B) ->
  210. lists:filter(fun(X) -> lists:member(X, A) end, B).
  211. %%并集
  212. get_unite_list(A, B) when is_list(A) andalso is_list(B) ->
  213. C = A ++ B,
  214. lists:usort(C).
  215. %%差集
  216. get_subtract_list(A, B) when is_list(A) andalso is_list(B) ->
  217. Insection = get_intersection_list(A, B),
  218. Unite = get_unite_list(A, B),
  219. lists:filter(fun(X) -> lists:member(X, Insection) =:= false end, Unite).
  220. merge(L) ->
  221. merge(L, []).
  222. merge([{Key, Value} | T], AccList) ->
  223. NewAccList =
  224. case lists:keyfind(Key, 1, AccList) of
  225. false ->
  226. [{Key, Value} | AccList];
  227. {Key, OldValue} ->
  228. lists:keyreplace(Key, 1, AccList, {Key, OldValue + Value})
  229. end,
  230. merge(T, NewAccList);
  231. merge([], AccList) ->
  232. AccList.