Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Elixir Using the Capture Operator

sum = &(&1 + &2) 
IO.puts(sum.(1, 2))

Elixir Anonymous Functions

sum = fn (a, b) -> a + b end
IO.puts(sum.(1, 5))

Need to Prepend with Elixir

alias List, as: String
#Now when we use String we are actually using List.
#To use the string module: 
IO.puts(Elixir.String.length("Hello"))

Elixir Aliases

alias String, as: Str
IO.puts(Str.length("Hello"))

Elixir Scripted Mode

defmodule Math do
   def sum(a, b) do
      a + b
   end
end
IO.puts(Math.sum(1, 2))

Elixir Maps with Atom keys

map = %{:a => 1, 2 => :b} 
IO.puts(map.a) 

Elixir Maps Pattern Matching

%{:a => a} = %{:a => 1, 2 => :b}
IO.puts(a)

Elixir Maps Updating a Value

map = %{:a => 1, 2 => :b}
new_map = %{ map | a: 25}
IO.puts(new_map[:a])

Elixir Maps Inserting a key

map = %{:a => 1, 2 => :b}
new_map = Dict.put_new(map, :new_val, "value") 
IO.puts(new_map[:new_val])

Elixir Maps Accessing a key

map = %{:a => 1, 2 => :b}
IO.puts(map[:a])
IO.puts(map[2])

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.