Erlang - sgml/signature GitHub Wiki
Hello World
-module(my_module).
-export([my_function/1]).
my_function(X) ->
io:format("Hello, ~p!~n", [X]).
Compilation
$ erl
Erlang/OTP 23 [erts-11.1.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe] [dtrace]
1> c(my_module).
{ok,my_module}
Command Line Interface
2> my_module:my_function("world").
Hello, "world"!
ok
Process Registration
Pid = spawn(fun() -> receive stop -> ok end end),
register(my_process, Pid).
Regex Example
-module(phone_validator).
-export([validate_phone_number/1]).
validate_phone_number(Number) ->
Regex = "\\(?\\d{3}\\)?[ .-]?\\d{3}[ .-]?\\d{4}",
re:run(Number, Regex, [{capture, first, list}]).
Signal Handler from Python
Python
import os
import signal
import subprocess
def handle_sighup(signum, frame):
# Send a message to the Erlang process
subprocess.run(["erl", "-noshell", "-sname", "signal_handler", "-s", "my_module", "handle_sighup", str(erlang_pid)])
# Start the Erlang process
erlang_process = subprocess.Popen(["erl", "-noshell", "-s", "my_module", "start"])
erlang_pid = erlang_process.pid
# Set up the signal handler for SIGHUP
signal.signal(signal.SIGHUP, handle_sighup)
# Keep the Python script running
try:
erlang_process.wait()
except KeyboardInterrupt:
pass
Erlang
-module(my_module).
-export([start/0, handle_sighup/1]).
start() ->
register(sighup_handler, self()),
loop().
loop() ->
receive
{sighup} ->
io:format("Received SIGHUP signal~n"),
loop();
_ ->
loop()
end.
handle_sighup(Pid) ->
{ok, Pid} = string:to_integer(Pid),
erlang:send(Pid, {sighup}),
halt().
Tutorials
- https://www.erlang.org/doc/getting_started/conc_prog.html
- https://www.npmjs.com/package/@otpjs/core
- https://github.com/silviucpp/erlkaf
- https://github.com/uhub/awesome-erlang/blob/master/README.md
- https://medium.com/erlang-battleground/distributed-caching-in-elixir-using-nebulex-9af589186caa
- https://hexdocs.pm/mix/
- https://underjord.io/finally-using-your-raspberry-pi-and-elixir.html
- http://rosettacode.org/wiki/Accumulator_factory#LFE
- https://github.com/lfe/docs/issues/95
Examples
- https://daveho.github.io/2012/12/05/parallel-mandelbrot-in-erlang.html
- https://github.com/ghulette/mandelbrot-erlang/tree/master
Glossary
Fault Tolerant: We create a single actor for each functional task, and each actor or thread has it's own supervisor thread. If an actor fails to execute it's task, it suspends itself and all of its children and sends an exception to its supervisor. The supervisor can then work on a recovery strategy. Actors and supervisors fail gracefully, and all the failures can be ultimately managed by the Elixir/Erlang virtual machine (called BEAM).
Scalable: Because the system has a set of dedicated actors for each functional module and the actors have very little overhead associated with them, it is easy to spawn new actors with an increase in the workload for any specific module. The number of actors is only limited by the resources available on the physical machine.
Supervisor: a process that supervises other processes called child processes. A child process can either be another supervisor or a worker process. Worker processes are normally implemented using one of the gen_event, gen_server, or gen_statem behaviors.
Exceptions: A process will crash and restart after an unhandled exception. Processes can monitor other processes and detect process terminations. A run-time error can also be emulated by calling erlang:error(Reason)
or erlang:error(Reason, Args)
.
BIF: Built-in Function
A Built-in Function(BIF) is the opposite of a User Defined Function (UDF)
Macros
Macros are global flags. For example, erlc -DNOASSERT=true *.erl
disables assertions
OTP: Open Telecom Platform
References
- https://www.erlang.org/doc/apps/stdlib/stdlib.pdf
- https://www.erlang.org/doc/apps/erts/erts.pdf
- https://www.erlang.org/docs/24/pdf/otp-system-documentation.pdf
- https://www.erlang.org/doc/apps/kernel/kernel.pdf
- https://www.erlang.org/doc/reference_manual/errors.html
- http://erlang.org/doc/apps/xmerl/xmerl_xs_examples.html
- http://erlang.org/doc/reference_manual/data_types.html
- https://www.erlang.org/doc/man/digraph_utils.html#depth_first_traversal
- https://www.erlang.org/doc/man/lists.html
- http://erlang.org/doc/apps/stdlib/io_protocol.html
- https://erlang.org/course/concurrent_programming.html#timeouts
- http://erlang.org/doc/man/global.html
- https://www.erlang.org/doc/efficiency_guide/advanced.html
- https://perugini.cps.udayton.edu/teaching/courses/Spring2017/cps499/Languages/papers/Elixir.pdf
- https://par.nsf.gov/servlets/purl/10332848
- https://ee.usc.edu/stochastic-nets/docs/backpressure.pdf