Skip to content

Commit

Permalink
Add Erlang module examples to the introductory notebook (#2831)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonatan Kłosko <[email protected]>
  • Loading branch information
fnchooft and jonatanklosko authored Oct 18, 2024
1 parent efbc36a commit 9c4b381
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions lib/livebook/notebook/learn/intro_to_livebook.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand All @@ -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.

Expand Down

0 comments on commit 9c4b381

Please sign in to comment.