Like every language, Elixir has drawers full of parts that are fun to peruse, and there are many more available through Erlang.
These are a few of the more common ones, all represented using Elixir calling conventions. If you want much much more, see the erlang documents.
You can use most Elixir functions from the shell, but the commands shown in Table A-1 are ones that are exclusive to the shell.
Command | Action |
---|---|
|
Compiles the specified Erlang file. |
|
Compiles the specified file and puts object code in the directory specified by |
|
Lists files at the current location. |
|
Lists files at the specified |
|
Changes to the specified |
|
Gets the present working directory. |
|
Clears the screen. |
|
Prints list of available helpers. |
|
Prints help for the specified |
|
Loads given |
|
Lists all loaded modules. |
|
Recompiles and reloads the given |
|
Prints a list of all commands and returned values for this session. |
|
Retrieves +n+th output value from shell session. |
|
Flushes all messages sent to the shell. |
There are a few Elixir terms you can’t use outside of their intended context.
The Elixir compiler will wonder what you’re trying to do if you use certain keywords as function names (see a list of these words in Table A-2. It will try to treat your functions as if they were code, and you can get very strange errors. After all, you should be able to have something called inlist
, right?
after |
and |
catch |
do |
else |
end |
false |
fn |
in |
inbits |
inlist |
nil |
not |
or |
rescue |
true |
when |
xor |
The answer is simple: use something else.
While the following aren’t reserved words, there are also a few atoms commonly used in return values. It’s probably best to use them only in the circumstances where they’re normally expected.
Atom | Means |
---|---|
|
Normal exit to a method. (Does not mean that whatever you asked for succeeded.) |
|
Something went wrong. Typically accompanied by a larger explanation. |
|
A value hasn’t been assigned yet. Common in record instances. |
|
A reply is included with some kind of return value. |
|
No return value is included. A response of some sort may come, however, from other communication. |
|
Used in OTP to signal that a server should stop, and triggers the |
|
Returned by OTP supervisor process that can’t start a child. |
Operator | Operator | Description |
---|---|---|
|
|
Logical and |
|
|
Logical or |
|
|
Unary logical not |
|
(No equivalent) |
Logical xor |
The logical not
operator has the highest precedence. and
, &&
, or
, and ||
are short-circuit operators. If they don’t need to process all the possibilities in their arguments, they stop at the first one that gives them a definite answer.
Operators in the first column require their argument(s) to be Boolean. Operators in the second column will accept any expression, with any value that is not false
or nil
treated as true
. Because of short-circuiting, the &&
and ||
operators will return whichever value “decided” the ultimate true or false value. For example, nil || 5
returns 5
, and nil && 5
returns nil
.
Operator | Description |
---|---|
|
Equal to |
|
Not equal to |
|
Less than or equal to |
|
Less than |
|
Greater than or equal to |
|
Greater than |
|
Exactly equal to |
|
Exactly not equal to |
You can compare elements of different types in Elixir. The relationship of types from “least” to “greatest” is:
number < atom < reference < fn < port < pid < tuple < list < bit string
Within number
, you can compare integers and floats except with the more specific ===
and !==
operators, both of which will return false
when you compare numbers of different types.
You can also compare tuples even when they contain different numbers of values. Elixir will go through the tuples from left to right and evaluate on the first value that returns a clear answer.
Operator | Description |
---|---|
|
Unary + (positive) |
|
Unary - (negative) |
|
Addition |
|
Subtraction |
|
Multiplication |
|
Floating-point division |
To calculate integer division and integer remainder, use the div
and rem
functions. Thus, div(17, 3)
yields 5, and rem(17, 3)
yields 2.
Function | Operator | Description |
---|---|---|
|
|
Unary bitwise not |
|
|
Bitwise and |
|
|
Bitwise or |
|
|
Arithmetic bitwise xor |
|
|
Arithmetic bitshift left |
|
|
Bitshift right |
If you wish to use these operators and functions, your code must use Bitwise
.
Operator | Associativity |
---|---|
Unary |
Not associative |
|
Right |
|
Right |
|
Right |
|
Left |
|
Left |
|
Left |
|
Left |
|
Left |
|
Left |
|
Left |
|
Left |
|
Left |
|
Left |
|
Right |
|
Right |
|
Left |
|
Right |
|
Right |
|
Right |
|
Right |
|
Left |
|
Right |
|
Left |
|
Not associative |
The highest priority operator in an expression is evaluated first. Elixir evaluates operators with the same priority by following associative paths. (Left associative operators go left to right, and right associative operators go right to left.)
Elixir allows only a limited subset of functions and other features in guard expressions, going well beyond a “no side effects” rule to keep a simple subset of possibilities. The list of allowed components includes the following:
true
Other constants (regarded as false
)
Term comparisons (Table A-5)
Boolean expressions and these logical operators: and
and or
The following functions: hd/1
, is_atom/1
, is_binary/1
, is_bitstring/1
, is_boolean/1
, is_float/1
, is_function/1
, is_function/2
, is_integer/1
, is_list/1
, is_number/1
, is_pid/1
, is_port/1
, is_record/1
, is_record/2
, is_reference/1
, is_term/2
, is_tuple/1
Function | Use |
---|---|
|
The constant pi |
|
Sine |
|
Cosine |
|
Tangent |
|
Inverse sine (arcsine) |
|
Inverse cosine (arcosine) |
|
Inverse tangent (arctangent) |
|
Arctangent that understands quadrants |
|
Hyperbolic sine |
|
Hyperbolic cosine |
|
Hyperbolic tangent |
|
Hyperbolic arcsine |
|
Hyperbolic arccosine |
|
Hyperbolic arctangent |
|
Exponential function |
|
Natural logarithm (base e) |
|
Logarithm (base 10) |
|
First argument to the second argument power |
|
Square root |
|
Error function |
|
Complementary error function |
Arguments for all trigonometric functions are expressed in radians. To convert degrees to radians, divide by 180 and multiply by pi.
The erf/1
and erfc/1
functions may not be implemented in Windows. The Erlang documentation also warns more broadly that “Not all functions are implemented on all platforms,” but these come directly from the C-language libraries.
Function | Returns | Use |
---|---|---|
|
|
Side effects specified in function |
|
New list |
Apply function to list values |
|
Subset |
Creating list where function returns |
|
Boolean |
Returns |
|
Boolean |
Returns |
|
Subset |
Collects the head of the list until the function is |
|
Subset |
Deletes the head of the list until the function is |
|
Accumulator |
Passes function list value and accumulator, forward through list |
|
Accumulator |
Passes function list value and accumulator, backward through list |
|
Tuple of two lists |
Split list based on function |
Chapter 8 describes these in greater detail.
Sequence | Produces |
---|---|
|
Double quote |
|
Single quote |
|
Backslash |
Backspace |
|
|
Delete |
|
Escape |
|
Form feed |
|
Newline |
|
Carriage return |
|
Space |
|
Tab |
|
Vertical tab |
|
Character in hex |
|
Characters in hex, where X… is one or more hexadecimal characters |
|
Control-A to control-Z |
Sigil | Meaning |
---|---|
|
Returns a list of characters |
|
Returns a regular expression |
|
Returns a binary string |
|
Returns a list of words |
Sigils created with lowercase letters will use escaping and interpolation as usual; those created with uppercase letters will be created exactly as written, with no escaping or interpolation.
atom() |
binary() |
float() |
fun() |
integer() |
list() |
tuple() |
---|---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
The type String.t()
is used for Elixir binaries; the type string()
is used for Erlang’s strings stored as a list of characters. For more, see the user manual.
18.226.133.49