conspectsJose ValimJoe Armstronggrandpa bookimmutablitymixiex


Elixir is a functional, meta-programmable language that is built on top of the Erlang VM

Elixir has no loops

most of the functions from the List module are greedy - the entire result is returned once the function is invoked. but how you would represent an infinite stream of data? how could you read data from a remote source if you have no knowledge when the data feed ends? Elixir has Streams

in OOP we create objects - in Elixir, we spawn processes. each process can communicate with other processes via message passing. the receiver of the message can choose to act upon the message if it understands it. otherwise, the message is simply ignored

you are free to use any Erlang library. Elixir is still Erlang under the hood – both of these languages share the same byte code. and you could call ANY Erlang code from Elixir:

        iex> :erlang.localtime
        {{2013, 10, 22}, {0, 14, 29}}
        iex> :random.uniform
        0.7230402056221108
        iex> :crypto.md5("Using crypto from Erlang OTP")
        <<192,223,75,115,...>>

for debugging:

       IO.inspect var  

mix is a build tool that allows you to easily create projects, manage tasks, run tests and more:

$ mix new my_app
$ cd my_app
$ mix test

Finished in 0.04 seconds (0.04s on load, 0.00s on tests)
1 tests, 0 failures

mix is also able to manage dependencies and integrates nicely with the hex package manager, which provides dependency resolution and the ability to remotely fetch packages


Elixir's interactive shell iex is able to leverage many aspects of the language and platform

$ iex
...
iex> t Enum                # prints types defined in the module Enum
...
iex> h IEx.pry             # prints the documentation for IEx pry functionality
...
iex> c "my_file.ex"        # compiles a file
...
iex> r(ModuleName)         # recompiles and reloads module 
...

Elixir is ...

... like Lisp

defmodule Dijkstra do

  def gcd(m, n) do
    cond do
      m == n -> m
      m > n  -> gcd(m - n, n)
      true   -> gcd(m, n - m)
    end
  end
  
end

... like Perl

  %{ key => value , ... }     for hash        # but in Perl - %( key => value, ... )

       iex> myh = %{:a => 12, :b => 13}
       %{a: 12, b: 13}
       iex> myh.a
       12
       iex> myh.b
       13


  &()    for function

       iex> 1..5 |> Enum.map(&(&1 + 3))
        [4, 5, 6, 7, 8]
       iex> 1..5 |> Enum.map(fn x -> x + 3 end)
        [4, 5, 6, 7, 8]


  use    for external Module
  
       def ModuleName
       do 
         use AnotherModule
         ...
       end

... like Ruby

  def funcname params       
  do                                 # but in Ruby - without 'do'
    ...
  end

  --------------------------
  ruby        elixir
  ---------------------------
  :symbol     :atom
  param:      param:


  function params without brackets but with commas
  last param - code block with do ...  end but without preceeding comma


private function:
  
  defp funcname param
  do
    ...
  end

... unlike Erlang

      ---------------------------------------------------------------------------
      erlang                                elixir
      ---------------------------------------------------------------------------      
      vars start with      Capital          vars start with        sMALL
      atoms start with     sMALL            atoms start with       :s | Mall
      lambdas       fun -> ... end          lambdas            fn -> ... end
      ---------------------------------------------------------------------------
      -behaviour (application).             use Application
      -behaviour (supervisor).              use Supervisor
      -behaviour (gen_server).              use GenServer
      -behaviour (gen_event).               use GenEvent
               -                            use Agent
               -                            use Task
      -include_lib(".../eunit.hrl").        use ExUnit.Case, async: true
      ----------------------------------------------------------------------------
            Pid ! {hello}.                  send pid , {:hello}
      ----------------------------------------------------------------------------