elixir KERNEL 3 Flashcards
to_charlist(:hello)
=> ‘hello’
x = %{“data” => %{user: %{id: 1}}}
pop_in(x[“data”][:user][:id])
=> {1, %{“data” => %{user: %{}}}}
trunc(-1.45)
=> -1
~w(cat #{“ dog bird “})
=> [“cat”, “dog”, “bird”]
body = %{“data” => %{id: 1}}
x = pop_in(body[“data”][:id])
y = pop_in(body, [“data”, :id])
x == y
=> true
~w(cat #{:dog} bird)
=> [“cat”, “dog”, “bird”]
tl([1, 2, 3])
=> [2, 3]
send self(), “hello”
=> “hello”
bit_size(<<1, 2, 3>>)
=> 24
unless 1 + 2 == 3, do: “hey”
=> nil
binary_part(“flashcards”, 0, 5)
=> “flash”
binary_part(“hello you”, 9, -3)
=> “you”
unless 1 + 2 == 3 do
“hello”
else
“good bye”
end
=> “good bye”
unless 1 + 2 == 5, do: “hey”
=> “hey”
match?(“h”, “h”)
=> true
match?({:a, 1}, {:a, 1})
=> true
bit_size(<<1::2, 2::3>>)
=> 5
Atom.to_string :hello |> String.upcase
** (FunctionClauseError)
no function clause matching in String.upcase/2
*Note: |> has higher precedence.
Therefore :hello is applied to String.upcase first.
x = %{“data” => %{user: %{id: 1}}}
pop_in(x, [“data”, :user, :id])
=> {1, %{“data” => %{user: %{}}}
x = %{“y” => %{user: %{id: 1}}}
put_in(x[“y”][:user][:id], 2)
=> %{“y” => %{user: %{id: 2}}}
try do
raise “hello”
rescue
e in [RuntimeError] -> “caught you!”
end
=> “caught you!”
tuple_size({1, “h”, :a})
=> 3
var = “bon”
Regex.match?(~r/bbon/, “rib#{var}”)
=> true
port = Port.open(
{:spawn, “cat”},
[:binary]
)
is_port(port)
=> true
~S(b#{o}n)
=> “b#{o}n”
*Note: Returns a string without escaping characters
and without interpolations.
raise(ArgumentError, message: “Error details”)
** (ArgumentError)
Error details
nil || 2
=> 2
*Note: Returns the second expression only if the first one is false or nil
d = “dog”
~w(cat #{d} bird)
=> [“cat”, “dog”, “bird”]
*Note: Returns a list of “words” split by whitespace. Character unescaping and interpolation happens for each word.
x = 1
match?(^x, 1)
=> true
~N[2015-01-13T13:00:07.001]
=> ~N[2015-01-13 13:00:07.001]
*Note: Sigil ~N for naive date times
Enum.empty?([1]) || 1
=> 1
*Note: Returns the second expression only if the first one is false or nil
function_exported?(
Calc,
:add,
2
)
=> false
*Note: Returns true if module is loaded and contains a public function with the given arity.
~W(cat dog bird)a
=> [:cat, :dog, :bird]
*Note: modifier a - words in the list are atoms.
~w(cards config test.exs)
=> [“cards”, “config”, “test.exs”]