-
Notifications
You must be signed in to change notification settings - Fork 36
/
elixir.exs
36 lines (31 loc) · 675 Bytes
/
elixir.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
defmodule MyModule do
@moduledoc """
This is a built-in attribute on a example module.
"""
# Struct
defstruct name: nil, age: 0, height: 0
# Function
def function_one do
{:ok, "one"}
end
# Pattern matching
def function_two do
{:ok, msg} = function_one
IO.puts msg
end
# Try/Rescue block
def function_three do
try do
raise "some error"
rescue
RuntimeError -> "rescued a runtime error"
_error -> "this will rescue any error"
end
end
# Pipes
def function_four(start, finish) do
Range.new(start, finish)
|> Enum.map(fn x -> x * x end)
|> Enum.filter(fn x -> rem(x, 2) == 0 end)
end
end