-
Notifications
You must be signed in to change notification settings - Fork 0
erlang elixir Notes
- Working with Erlang
- Definitions
- Courses
- Working with Elixir
- Understanding documentation in Elixir
-
Working with
mix
- iex Sessions
- Useful Links
- Working with Phoenix framework
- Useful Links
To make a comment, ie. a programmers comment in a erlang source code file
% The percent character denotes a programmers in Erlang
The unofficial package manager for Erlang is widely considered rebar3
To install rebar3 on macOS from git source
git clone [email protected]:erlang/rebar3.git
cd rebar3
./bootstrap
./rebar3 local install
If erlang was installed using asdf version manager, then symlink
$HOME/.cache/rebar3/bin/rebar3
to$HOME/.asdf/shims/rebar3
ln -sf $HOME/.cache/rebar3/bin/rebar3 $HOME/.asdf/shims/rebar3
For more information on working with rebar3, see rebar3.org
To create a new app using rebar3
rebar3 new app [mr-fancy-rebar3-app]
To add dependencies to a rebar3 app edit rebar.config
and dependencies such as cowboy within deps.
- [%]
figure out how to make a comment within an erlang source code file.
erts Erlang Run-Time System
- In Elixir ALL data is immutable.
-
.ex
files need to be compiled into bytecode before they can run. - Module names in Elixir are camel case, ie.
MyModule
- Function names in Elixir are snake case, ie.
my_function
It is to my understanding that when reading something like the below
call(conn, level)
Callback implementation for Plug.call/2
...when calling Plug.call/2 the call is going to require 2 arguments.
To create a new Elixir project using mix
mix new [project-name] --module [PROJECTNAME]
Ex
mix new cow_servy --module CowServy
To build a simple Hello, World! web server in elixir
- Create an elixir project using mix
- Add cowboy and plug as dependencies to
mix.exs
- Install the included dependencies
mix deps.get
-
Edit
/lib/cow_servy.ex
and add these contents to the file. -
Build the project
iex -S mix
- Compile
/lib/cow_servy.ex
if needed, otherwise start the web server process
iex> c "lib/cow_servy.ex"
iex> {:ok, _} = Plug.Adapters.Cowboy2.http CowServy, []
- An alternative way to run compile and run the web server
iex> Plug.Adapters.Cowboy2.http(CowServy, %{})
- Access local server at http://localhost:4000
The iex session does not need to be reloaded when expiermenting, ie. making changes to a .ex
file, as Elixir, iex, and mix support compiling within the same iex session.
Ex
If a change is made to lib/cow_servy.ex
the module can be reloaded with the below iex command
iex> r(CowServy)
The simple CowServy module will need to be started before changes can be reflected a browser session.
To start CowServy
iex> Plug.Adapters.Cowboy2.http(CowServy, %{})
To identify the PID process launched with with the above command
iex> self()
TODO
- figure out how to kill an elixir process started from iex while in iex.
- figure out how to display the response time within the iex session.
- the r key can be used in an interactive elixir session iex when a change is made to a
.ex
file to update and recompile the app.
To start a mix app and keep running, ie. it won't exit
mix run --no-halt
An elixir script file, ie. .exs
can be run with the below command
mix run /path/to/mr-fany-elixir-script.exs
For more information on scaffolding a mix application see
- GitHub - Great π Reference for Elixir
- Wikipedia - erlang
- Wikipedia - elixir
- elixirnation.io - search for all things elixir
- elixirschool.com
- GitHub - elixir style guide
- medium.com - GraphQL with Elixir/Phoenix and Absinthe
To generate an Elixir > Phoenixframework, ie. Phoenix 1.4 project
mix phx.new --app blog \
Blog Article articles title:string \
--no-webpack --no-html
A context can be singular or plural; i.e. ex: blog ex: sales
To generate a basic blog app using Phoenix
mix phx.gen.json blog Article articles title:string
To create a skeleton phx v1.3 API
mix phx.new my_fancy_pants --no-html --no-brunch
the proper way to generate a context & schema
mix phx.gen.json Blog Article articles title:string
- Blog - context
β οΈ the context name must be capitalized; i.e. Blog
- Article - schema
- articles - table_name