From 9c4b381664c7ed438d78780ca2e34e04ed447ea8 Mon Sep 17 00:00:00 2001 From: "Fabian N.C. van 't Hooft" Date: Fri, 18 Oct 2024 03:25:29 -0300 Subject: [PATCH] Add Erlang module examples to the introductory notebook (#2831) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jonatan Kłosko --- .../notebook/learn/intro_to_livebook.livemd | 41 +++++++++++++++---- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/lib/livebook/notebook/learn/intro_to_livebook.livemd b/lib/livebook/notebook/learn/intro_to_livebook.livemd index f42ddd2ed..35ded6461 100644 --- a/lib/livebook/notebook/learn/intro_to_livebook.livemd +++ b/lib/livebook/notebook/learn/intro_to_livebook.livemd @@ -155,8 +155,6 @@ $$ S(x) = \frac{1}{1 + e^{-x}} = \frac{e^{x}}{e^{x} + 1} $$ - - 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.