Add Erlang module examples to the introductory notebook (#2831)

Co-authored-by: Jonatan Kłosko <jonatanklosko@gmail.com>
This commit is contained in:
Fabian N.C. van 't Hooft 2024-10-18 03:25:29 -03:00 committed by GitHub
parent efbc36a6f7
commit 9c4b381664
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -155,8 +155,6 @@ $$
S(x) = \frac{1}{1 + e^{-x}} = \frac{e^{x}}{e^{x} + 1}
$$
<!-- livebook:{"force_markdown":true} -->
To write your own, put your math expressions between \$ signs for inline math
or \$\$ if you want display math. You can double click the formulas above to see
how they are written.
@ -190,13 +188,13 @@ you can leverage all of the dependency management and smart cell features
outlined in the previous sections. In particular, integration between
Erlang and Elixir will happen as follows:
* Variables in Elixir are available in Erlang cells in camel-case
fashion. `x` in Elixir becomes `X` in Erlang. `foo_bar` becomes
`FooBar`;
* Variables in Elixir are available in Erlang cells in camel-case
fashion. `x` in Elixir becomes `X` in Erlang. `foo_bar` becomes
`FooBar`;
* Variables in Erlang are available in Elixir cells in underscored
fashion. `X` in Erlang becomes `x` in Elixir. `FooBar` becomes
`foo_bar`;
* Variables in Erlang are available in Elixir cells in underscored
fashion. `X` in Erlang becomes `x` in Elixir. `FooBar` becomes
`foo_bar`;
For example, to print all of the cats defined at the top of the notebook,
but in Erlang:
@ -205,6 +203,33 @@ but in Erlang:
[io:format("~ts", [Cat]) || Cat <- Cats].
```
### Defining modules
You can also define Erlang modules, one per cell, as you would in an `.erl` file.
```erlang
-module(direction).
-export([north/0, east/0, west/0, south/0]).
north() -> {ok, north}.
east() -> {ok, east}.
west() -> {ok, west}.
south() -> {ok, south}.
```
Macros, includes, and similar module feature are supported.
Once the module is defined, you can call the functions as usual:
```erlang
{ok, N} = direction:north(),
{ok, E} = direction:east(),
{ok, W} = direction:west(),
{ok, S} = direction:south(),
{N, E, W, S}.
```
We are just beginning the Erlang integration and contributions to
further enrich it are welcome.