Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Alquimia 2.2

notebook/notebook-lesson2.livemd

Alquimia 2.2

Lists

good_the_office_seasons = [1, 2, 3, 4, 5, 6, 7]
all_the_office_seasons = good_the_office_seasons ++ [8, 9]
bad_the_office_seasons = all_the_office_seasons -- good_the_office_seasons
good_the_office_seasons -- all_the_office_seasons
first_season = hd(all_the_office_seasons)
remaining_seasons = tl(all_the_office_seasons)
[first | remaining] = all_the_office_seasons
remaining
["a" | ["b", "c", "d"]] == ["a", "b", "c", "d"]
["a" | ["b" | ["c" | ["d" | []]]]]
# List.flatten([1, ["b"], [:ok], [""]]) |> IO.inspect

# List.last(["first", "second", "last"]) |> IO.inspect

# List.first(["first", "second", "last"]) |> IO.inspect

# List.delete(["a", "b", "c", "a"], "a") |> IO.inspect

List.to_tuple([:error, :not_found]) |> IO.inspect()

Tuples

name_inside = fn name ->
  if name in ["Rick", "Ana", "khaleesi"], do: {:ok, name}, else: {:error, :not_found}
end

name_inside.("khaleesi1")
Tuple.to_list({1, 2, 3})
Tuple.append({"Luke", "Leia", "Han"}, "Yoda")

Keyword Lists

kl = [name: "John", lastname: "McLane"]

[name: "John", lastname: "McLane"] == []

[name: "John", lastname: "McLane", lastname: "McAvoy"]

[{:name, "John"}, {:lastname, "McLane"}] == [name: "John", lastname: "McLane"]

[name: "John", lastname: "McLane"] == [lastname: "McLane", name: "John"]

Maps

IO.inspect(%{:a => 5} == %{a: 5})

%{1 => 5}

%{"name" => "Rick", "name" => "Rickson"}
my_map = Map.new(a: 5, b: 10)

my_map = Map.put(my_map, :c, 15)

value = Map.get(my_map, :a)
value == my_map[:a]
value == my_map.a

Map.has_key?(my_map, :d)

Map.drop(my_map, [:a, :c])

Map.update(my_map, :a, 1, fn value -> value * 2 end)

Map.take(my_map, [:a, :b])

%{my_map | a: 25}

Operadores

10 + 20
350 - 15
350.0 - 12
50 + 100.0
50 * 2
50 / 2
div(50, 10)
rem(10, 3)

Operadores booleans

true and false
true and true
false or false
true or false
not true
not_a_string = not String.valid?("string")
false && ""
"" && 15
nil || 100
false || 'charlist'
false || nil
!1
!nil

Operadores de comparação

10 != 10
10 == 10
10.0 == 10
10.0 === 10
11 >= 10
10 <= 11
20 > 30
11 < 12
# Everyhting in Elixir is comparable
# number < atom < reference < function < port < pid < tuple < map < list < bitstring
<<0>> > []