Last active
December 27, 2019 08:47
-
-
Save melekes/e7ad364d776adffb0e76 to your computer and use it in GitHub Desktop.
Erlang snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-spec do(any(), [fun((...) -> any())]) -> any(). | |
do(Arg, []) -> | |
Arg; | |
do(Arg, [Fun | Funs]) -> | |
case Fun(Arg) of | |
{error, Data} -> | |
Data; | |
Data -> | |
do(Data, Funs) | |
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-spec lhttpc_request_with_retry(fun(() -> lhttpc:result()), integer()) -> lhttpc:result() | {error, no_attempts_left}. | |
lhttpc_request_with_retry(_, 0) -> | |
{error, no_retries_left}; | |
lhttpc_request_with_retry(RequestFun, AttemptsLeft) -> | |
Res = RequestFun(), | |
case Res of | |
{ok, _} -> | |
Res; | |
{error, _} -> | |
N = ?ATTEMPTS_TO_CONTACT - AttemptsLeft, | |
timer:sleep(1000*N), | |
lhttpc_request_with_retry(RequestFun, AttemptsLeft-1) | |
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f(Percentiles). | |
Percentiles = fun(Numbers) -> | |
Percentile = fun(List, Size, Perc) -> | |
Element = round(Perc * Size), | |
lists:nth(Element, List) | |
end, | |
Len = length(Numbers), | |
Sorted = lists:sort(Numbers), | |
[{trunc(Perc*100), Percentile(Sorted, Len, Perc)} || | |
Perc <- [0.50, 0.75, 0.90, 0.95, 0.99, 0.999]] | |
end. | |
%% Percentiles([3, 7, 2, 6.25, ...]). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% to prevent races | |
wait_until_finished(Pid) -> | |
exit(Pid, shutdown), | |
Ref = monitor(process, Pid), | |
receive | |
{'DOWN', Ref, process, Pid, _Reason} -> | |
ok | |
after 1000 -> | |
error(exit_timeout) | |
end. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-spec get_timestamp() -> integer(). | |
get_timestamp() -> | |
{Mega, Sec, Micro} = os:timestamp(), | |
(Mega*1000000 + Sec)*1000 + round(Micro/1000). % ms | |
iso8601({{Y,Mo,D}, {H,Mn,S}}) -> | |
<<(integer_to_binary(Y))/binary, $-, | |
($0 + (Mo div 10)), ($0 + (Mo rem 10)), $-, | |
($0 + (D div 10)), ($0 + (D rem 10)), $T, | |
($0 + (H div 10)), ($0 + (H rem 10)), $:, | |
($0 + (Mn div 10)), ($0 + (Mn rem 10)), $:, | |
($0 + (S div 10)), ($0 + (S rem 10)), $Z>>. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment