Learning Elixir

发布于 2018-03-25 16:33:44

Key Concepts

  • Agent: a simple abstraction around state
  • GenServer: client-server pattern
  • Supervisor: monitor other process
  • Dynamic supervisors
  • ETS (Erlang Term Storage)
  • Mix project
  • OTP: OTP stands for Open Telecom Platform, although it’s not that much about telecom anymore (it’s more about software that has the property of telecom applications, but yeah.)
  • Umbrella project
  • Task: Tasks are processes meant to execute one particular action throughout their lifetime, often with little or no communication with other processes. The most common use case for tasks is to convert sequential code into concurrent code by computing a value asynchronously

Mix makes a distinction between projects and applications.

When we say “project” you should think about Mix. Mix is the tool that manages your project. It knows how to compile your project, test your project and more.

When we talk about applications, we talk about OTP. Applications are the entities that are started and stopped as a whole by the runtime.

About Erlang: If half of Erlang’s greatness comes from its concurrency and distribution and the other half comes from its error handling capabilities, then the OTP framework is the third half of it.

mix command

If you’re familiar with Ruby, Mix is Bundler, RubyGems, and Rake combined.

Supervisor

4 supervision strategy:

  1. default: :rest_for_one
  2. :one_for_one
  3. :one_for_all
  4. :rest_for_one

ETS

Erlang Term Storage

Serveral API:

  • :ets.new
  • :ets.lookup
  • :ets.insert
  • :ets.delete

Serveral function names

Functions to be called when some action takes.

  • start_link
  • init
  • start

Dependencies and umbrella projects

Two types dependencies: internal and external. Mix supports both two types.

Versions definition: Version – Elixir v1.6.4

$ mix help | grep deps
mix deps              # Lists dependencies and their status
mix deps.clean        # Deletes the given dependencies' files
mix deps.compile      # Compiles dependencies
mix deps.get          # Gets all out of date dependencies
mix deps.tree         # Prints the dependency tree
mix deps.unlock       # Unlocks the given dependencies
mix deps.update       # Updates the given dependencies
mix hex.audit         # Shows retired Hex deps for the current project
mix hex.outdated      # Shows outdated Hex deps for the current project

Mix and Umbrella projects

$ mix new kv_umbrella --umbrella
* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating apps
* creating config
* creating config/config.exs

Your umbrella project was created successfully.
Inside your project, you will find an apps/ directory
where you can create and host many apps:

    cd kv_umbrella
    cd apps
    mix new my_app

Commands like "mix compile" and "mix test" when executed
in the umbrella project root will automatically run
for each application in the apps/ directory.

Create OTP application

$ mix new kv_umbrella --umbrella
$ cd kv_umbrella/apps
$ mix new kv_server --module KVServer --sup

Serveral paths

  • config
  • dependencies store path
  • dependencies lockfile
  • build output path

TCP Server

Doctests

~S```....\r\n```

Distributed tasks

Meta programming

Quote and unquote

Macros

Domain specific languages

Other libraries

  • Plug: It provides a specification for web application components and adapters for web servers. While not part of Elixir core, Plug is an official Elixir project.
  • Ecto: Ecto is an official Elixir project providing a database wrapper and integrated query language. With Ecto we’re able to create migrations, define schemas, insert and update records, and query them.
  • EEx
  • ETS
  • Mnesia: Mnesia is a heavy duty real-time distributed database management system.
  • Guardian: Guardian is a widely used authentication library based on JWT (JSON Web Tokens).
  • Poolboy: You can easily exhaust your system resources if you do not limit the maximum number of concurrent processes that your program can spawn. Poolboy is a widely used lightweight, generic pooling library for Erlang that addresses this issue.
  • Benchee: We can’t just guess about which functions are fast and which are slow - we need actual measurements when we’re curious.
  • Bypass: Bypass is described as “a quick way to create a custom plug that can be put in place instead of an actual HTTP server to return prebaked responses to client requests.” Under-the-hood Bypass is an OTP application that masquerades as an external server listening for and responding to requests.

Questions

Q: &1 in lambda?

A: lambda function’s positional parameter.

comments powered by Disqus