elixir KERNEL 2 Flashcards
is_map(%{})
=> true
What type of Error raised when executing? not nil
ArgumentError
*Note: Arguments must be a boolean
length([a: 1, b: 2])
=> 2
hd([5, 6, 7])
=> 5
is_float(1.2)
=> true
What type of Error raised when executing?
elem({}, 0)
ArgumentError
is_map(%Range{})
=> true
true and “hello”
=> “hello”
is_nil(nil)
=> true
to_string(12.3)
=> “12.3”
false and “hello”
=> false
rem(8, -7)
=> 1
elem({1, 2, :hello}, 1)
=> 2
apply(Kernel, :byte_size, [“cat”])
=> 3
is_function(fn(x) -> x + 1 end)
=> true
is_integer(28) == is_number(2.7)
=> true
tuple = {:clever, “bunny”}
put_elem(tuple, 0, “funny”)
=> {“funny”, “bunny”}
inspect(:hello)
=> “:hello”
byte_size(«1, 2»)
=> 2
inspect(‘bar’)
=> “‘bar’”
not false
=> true
inspect [1, 2, 3], limit: 2
=> “[1, 2, …]”
self() |> is_pid()
=> true
iex> x = 1
iex> binding()
iex => [x: 1]
*Note: Returns the binding for the given context as a keyword list
is_list([])
=> true
to_string([1, 2, 3])
=> «1, 2, 3»
true or false
=> true
inspect(15, base: :hex)
=> “OxF”
What type of Error raised when executing?
:hello or true
** (BadBooleanError)
expected a boolean on left-side of “or”,
got: :hello
to_string(:hello)
=> “hello”
map_size(%{a: 1})
=> 1
to_string(123)
=> “123”
max(1, :a)
=> :a
*Note: term ordering: number < atom < reference < function < port < pid < tuple < map < list < bitstring
round(4.49)
=> 4
node() |> is_binary()
=> false
*Note: Returns an atom representing the name of the local node. If the node is not alive, :nonode@nohost is returned instead
What type of error would be raised when evaluating?
1 and true
** (BadBooleanError)
expected a boolean on left-side of “and”,
got: 1
iex> x = 1
iex> x = 2
iex> binding()
=> [x: 2]
*Note: Returns the binding for the given context as a keyword list.
What type of error is raised when executing?
rem(8, 0.8)
** (ArithmeticError) bad argument in arithmetic expression
:erlang.rem(8, 0.8)
1 && 4 == 1 and 4
=> false
*Note: and accepts boolean as a first argument. && accepts any expression as the first argument
fun = fn x -> max(x, 3) end
apply(fun, [2])
=> 3
Agent.start(fn() -> [] end) |> is_tuple()
=> true
*Returns {:ok, #PID}
make_ref() |> is_reference()
=> true
if is_bitstring("bunny") do "bunny is a bitstring" else "not a bitstring" end
=> “bunny is a bitstring”
min(self(), :hello)
=> :hello
*Note: term ordering: number < atom < reference < function < port < pid < tuple < map < list < bitstring
if div(8, 2) == 3, do: “yes”
=> nil
*Note nil is returned when condition passed to if doesn’t return true
What type of Error raised when executing?
to_string({:ok, “hello”})
** (Protocol.UndefinedError)
protocol String.Chars not implemented for {:ok, “hello”} of type Tuple
inspect(
“hello” <> «0»,
binaries: :as_strings
)
=> “"hello\0"”
inspect(
“h” <> «0»,
binaries: :as_binaries
)
=> “«104, 0»”
iex> x = 1
iex> y = 2
iex> binding()
=> [x: 1, y: 2]
*Note: Returns the binding for the given context as a keyword list.
What type of Error is raised when executing?
hd([])
ArgumentError
true and 4 and 5
** (BadBooleanError)
expected a boolean on left-side of “and”,
got: 4
byte_size(«145::24, 5::4»)
=> 4
*Note: if the number of bits in bitstring is not divisible by 8, the resulting number of bytes will be rounded up (by excess)
is_function(fn(x) -> x + 1 end, 2)
=> false
*Note: Returns true if term is a function that can be applied with arity number of arguments; otherwise returns false
true or “hello”
=> true
*Note: If the first argument is true, true is returned; otherwise, the second argument is returned. Requires only the first argument to be a boolean since it short-circuits.