Skip to content

Instantly share code, notes, and snippets.

@Toshakins
Last active November 13, 2022 23:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Toshakins/410f104235af5f9bb13f to your computer and use it in GitHub Desktop.
Save Toshakins/410f104235af5f9bb13f to your computer and use it in GitHub Desktop.
Erlang
-module (ch13).
-compile(export_all).
timer() ->
% io:format("Hi from ~p~n", [self()]),
receive
_
-> io:format("Bye!~n"),
exit('Bye')
after
5000
-> %io:format("I'm still running~n"),
timer()
end.
timerJob() ->
[
{spawn(ch13, timer, []), ch13, timer},
{spawn(ch13, timer, []), ch13, timer},
{spawn(ch13, timer, []), ch13, timer},
{spawn(ch13, timer, []), ch13, timer},
{spawn(ch13, timer, []), ch13, timer},
{spawn(ch13, timer, []), ch13, timer},
{spawn(ch13, timer, []), ch13, timer},
{spawn(ch13, timer, []), ch13, timer},
{spawn(ch13, timer, []), ch13, timer},
{spawn(ch13, timer, []), ch13, timer},
{spawn(ch13, timer, []), ch13, timer}
].
loopty_loop() ->
receive
{'DOWN', _Ref, process, Pid, Why} ->
{Mod, Fun} = get(Pid),
io:format("Spawning ~p after ~p~n", [Pid, Why]),
erase(Pid),
P = spawn(Mod, Fun, []),
put(P, {Mod, Fun}),
monitor(process, P);
get ->
io:format("~p~n", [get()])
end,
loopty_loop().
multivizier(WorkerList) ->
lists:map(fun
({Pid, Mod, Fun}) ->
monitor(process, Pid),
put(Pid, {Mod, Fun})
end, WorkerList),
loopty_loop().

Erlang Lecture Notes

Error Handling

try FuncOrExpressionSeq of
  Pattern1 [when Guard1] -> Expressions1;
  Pattern2 [when Guard2] -> Expressions2;
  ...
catch
  ExceptionType1: ExPattern1 [when ExGuard1] -> ExExpressions1;
  ExceptionType2: ExPattern2 [when ExGuard2] -> ExExpressions2;
  ...
after
  AfterExpressions
end

ExceptionTypes are throw/1, error/1 and exit/1.

Shorthand:

try F
catch _:_ -> SaveExpression
after Cleaning

Ultimate shorthand, which ignores error/1 and exit/1 and behaves like catch _ -> ...:

catch F

Example

[{I, (catch 
  (fun
    (X) when is_number(X) -> X 
  end)(I))
} || I <- [1, "error"]].

It is also useful to get stack traces. Erlang way is to fail noisy and polite. Give a full information to a programmer but hide from a user.

Interfacing techniques

Erlang has multiple ways to interact with non-Erlang world:

  • Start a process outside ERTS(using port). This solution requires to define a protocol and write a driver in your language of choice.
  • Run a shell command, something like os:cmd('ls -al').
  • Run the code inside a virtual machine. This solution also got multiple choices:
    • Linked-in drivers. Similar to port, but requires some additional code. It can make a use of additional support like threading and message sending.
    • NIF(native implemented functions). Piece of synchronous code, must be small and fast. Like BIFs from standard library.
    • C-Nodes. C nodes are nodes implemented in C that obey the Erlang distribution protocol. A “real” distributed Erlang node can talk to a C-node and will think that the C-node is an Erlang node (provided it doesn’t try to do anything fancy on the C-node like sending it Erlang code to execute).
-module (strfun).
-export ([search/2]).
search(Pattern, Str) when is_list(Str), is_list(Pattern) ->
search_acc(Pattern, Str, 0).
search_acc([C|Pattern], [H|T], Pos) ->
if
C =:= H ->
search_acc(Pattern, T, Pos);
true ->
search_acc([C|Pattern], T, Pos + 1)
end;
search_acc(Pattern, [], _) when is_list(Pattern) ->
-1;
search_acc([], String, Pos) when is_list(String) ->
Pos.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment