Skip to content

erlang elixir Notes

Chris Jones edited this page Mar 17, 2019 · 3 revisions

Contents

Working with Erlang

To make a comment, ie. a programmers comment in a erlang source code file

% The percent character denotes a programmers in Erlang

Working with rebar3

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.

Erlang TODOs

  • [%] figure out how to make a comment within an erlang source code file.

Definitions

erts Erlang Run-Time System

Courses

Working with Elixir

  • 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

Understanding documentation in Elixir

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.

Working with mix

To create a new Elixir project using mix

mix new [project-name] --module [PROJECTNAME]

Ex

mix new cow_servy --module CowServy

Building a Simple web server in Elixir

To build a simple Hello, World! web server in elixir

  1. Create an elixir project using mix
  2. Add cowboy and plug as dependencies to mix.exs
  3. Install the included dependencies
mix deps.get
  1. Edit /lib/cow_servy.ex and add these contents to the file.

  2. Build the project

iex -S mix
  1. 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, []
  1. An alternative way to run compile and run the web server
iex> Plug.Adapters.Cowboy2.http(CowServy, %{})
  1. Access local server at http://localhost:4000

Elixir CowServy

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.

iex Sessions

  • 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

Useful Links

Screencasts

News

Deployment

Working with Phoenix framework

Update March 17 2019

To generate an Elixir > Phoenixframework, ie. Phoenix 1.4 project

mix phx.new --app blog \
Blog Article articles title:string \
--no-webpack --no-html

Random Notes

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

Useful Links

Testing Frameworks

API

GraphQL

Job Boards