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

Collections

2023_12_12_2.livemd

Collections

Lists

[1, 3, 4]
[1, 3, "a"]

Tuples

{1, 3, 4}

Maps

%{}
%{:A => 1}
%{"A" => 1}
%{:A => 1, :B => 3, :C => 45}
map = %{:A => 1, :B => 3, :C => 3, :A => 90}
Enum.map(map, fn x -> x end)
Enum.map(map, fn {x, _y} -> x end)
Enum.map(map, fn {_x, y} -> y end)
Enum.map(map, fn {key, value} -> {key, value * 2} end)
map
|> Enum.reduce(
  Map.new(),
  fn {key, value}, acc ->
    Map.put(acc, key, value * 2)
  end
)
[1, 2, 30]
|> Enum.reduce(1, fn x, acc -> acc * x end)

Keyword list

[{:A, 90}, {:B, 3}, {:C, 45}]
x = 3
x = 4
x
Integer.parse("10")
Integer.parse("10AAA")
Integer.parse("AA")
input = "a"

case Integer.parse(input) do
  :error -> "This '#{input}' is not a number"
  {number, ""} -> "Your number is #{number}"
  _ -> "This '#{input}' is not only a number"
end
x = [1, 2]
List.replace_at(x, 1, 300)
x
Enum.reduce_while(1..100, 0, fn x, acc ->
  if x < 5 do
    {:cont, acc + x}
  else
    {:halt, acc}
  end
end)
Enum.reduce_while(100..1, 0, fn x, acc ->
  if x > 95 do
    {:cont, acc + x}
  else
    {:halt, acc}
  end
end)
100 + 99 + 98 + 97 + 96
with {m, ""} <- Integer.parse("10"),
     {n, ""} <- Integer.parse("BB20") do
  m * n
end