Tutorials Point, posted on 30th Nov, 2017

Elixir Compilation

IO.puts(Math.sum(1, 2))

Tutorials Point, posted on 22nd Nov, 2017

Elixir Libraries Queue Module

q = :queue.new
q = :queue.in("A", q)
q = :queue.in("B", q)
{{:value, val}, q} = :queue.out(q)
IO.puts(val)
{{:value, val}, q} = :queue.out(q)
IO.puts(val)

Tutorials Point, posted on 22nd Nov, 2017

Elixir Libraries Math Module

# Value of pi
IO.puts(:math.pi())

# Logarithm
IO.puts(:math.log(7.694785265142018e23))

# Exponentiation
IO.puts(:math.exp(55.0))

#...

Tutorials Point, posted on 22nd Nov, 2017

Elixir Libraries Digraph Module

digraph = :digraph.new()
coords = [{0.0, 0.0}, {1.0, 0.0}, {1.0, 1.0}]
[v0, v1, v2] = (for c <- coords, do: :digraph.add_vertex(digraph, c))
:digraph.add_edge(digraph, v0, v1)
:digraph.add_edge(digraph, v1, v2)
for point <- :digraph.get_short_path(digraph, v0, v2) do 
   {x, y} = point
   IO.puts("#{x}, #{y}")
end

Tutorials Point, posted on 22nd Nov, 2017

Elixir Libraries Crypto Module

IO.puts(Base.encode16(:crypto.hash(:sha256, "Elixir")))

Tutorials Point, posted on 22nd Nov, 2017

Elixir Error Handling Exit

val = try do
   exit "I am exiting"
catch
   :exit, _ -> "not really"
end

IO.puts(val)

Tutorials Point, posted on 22nd Nov, 2017

Elixir Error Handling Throws

val = try do
   Enum.each 20..100, fn(x) ->
      if rem(x, 13) == 0, do: throw(x)
   end
   "Got nothing"
catch
   x -> "Got #{x}"
end

IO.puts(val)

Tutorials Point, posted on 22nd Nov, 2017

Elixir Errors in the Rescue Statement

err = try do
   1 + "Hello"
rescue
   RuntimeError -> "You've got a runtime error!"
   ArithmeticError -> "You've got a Argument error!"
end

IO.puts(err)

Tutorials Point, posted on 22nd Nov, 2017

Elixir Error Handling Rescuing Errors

err = try do
   raise "oops"
rescue
   e in RuntimeError -> e
end

IO.puts(err.message)

Tutorials Point, posted on 22nd Nov, 2017

Elixir Adopting a Behaviour

defmodule GreetBehaviour do
   @callback say_hello(name :: string) :: nil
   @callback say_bye(name :: string) :: nil
end

defmodule EnglishGreet do
   @behaviour GreetBehaviour
   def say_hello(name), do: IO.puts("Hello " <> name)
   def say_bye(name), do: IO.puts("Goodbye, " <> name)
end

defmodule SpanishGreet do
   @behaviour GreetBehaviour
   def say_hello(name), do: IO.puts("Hola " <> name)
   def say_bye(name), do: IO.puts("Adios " <> name)
end

EnglishGreet.say_hello("Ayush")
EnglishGreet.say_bye("Ayush")
SpanishGreet.say_hello("Ayush")
SpanishGreet.say_bye("Ayush")

1 2 3 4 5 6 7 8 Next
Advertisements
Loading...