Operators Reference
The following is the full list of operators in Halcyon, in order of their precedence:
Symbol | Description |
---|---|
(unary) - -. | arithmetic negation |
(unary) not | logical negation |
* *. | multiplication |
/ /. | division |
% | modulus |
+ +. | addition |
- -. | subtraction |
(function call) | |
>> << | function composition |
xor | logical XOR |
or | logical OR |
|> | function pipe |
== != <= >= < > | comparison |
and | logical AND |
; |
Currently, the comparison operators are only defined for primitive types. Comparing other data types will cause a crash.
Operators are regular functions that are defined in the std
module.
An operator's function can be accessed by surrounding it in parenthesis, like ( + ) 1 2 == 1 + 2
Always put whitespace around an operator when it is in parenthesis.
(*)
is parsed as the beginning of a comment, while ( * )
is the multiplication operator.
Integer and Real Operators
Because operators are functions, the operators for integers and reals are different.
Integers | Reals |
---|---|
+ | +. |
- | -. |
* | *. |
/ | /. |
% |
The ;
Operator
The ;
simply returns the value to the right.
It is useful for chaining together function calls with side effects.
Example
<span class='hljs-keyword'>do</span> <span class='hljs-title class_'>std</span><span class='hljs-punctuation'>::</span><span class='hljs-title function_'>println</span> <span class='hljs-string'>"old pond"</span><span class='hljs-punctuation'>;</span>
<span class='hljs-title class_'>std</span><span class='hljs-punctuation'>::</span><span class='hljs-title function_'>println</span> <span class='hljs-string'>"frog leaps in"</span><span class='hljs-punctuation'>;</span>
<span class='hljs-title class_'>std</span><span class='hljs-punctuation'>::</span><span class='hljs-title function_'>println</span> <span class='hljs-string'>"water's sound"</span><span class='hljs-punctuation'>;</span>
<span class='hljs-title class_'>std</span><span class='hljs-punctuation'>::</span><span class='hljs-title function_'>println</span> <span class='hljs-string'>" - Bashō'"</span>
The |>
Operator
|>
is the pipe operator.
This operator calls a function on the right with an argument on the left.
The pipe operator helps create function "pipelines", where the return value of one function becomes the parameter of the next function.
Example
<span class='hljs-keyword'>do</span> <span class='hljs-title class_'>format</span><span class='hljs-punctuation'>::</span><span class='hljs-title function_'>real</span> <span class='hljs-variable constant_'>3.14159</span>
<span class='hljs-comment'>(* Convert real to string *)</span>
<span class='hljs-operator'>|></span> <span class='hljs-title class_'>string</span><span class='hljs-punctuation'>::</span><span class='hljs-title function_'>concatenate</span> <span class='hljs-string'>"pi = "</span>
<span class='hljs-comment'>(* Prepend "pi = " *)</span>
<span class='hljs-operator'>|></span> <span class='hljs-title class_'>std</span><span class='hljs-punctuation'>::</span><span class='hljs-title function_'>println</span>
<span class='hljs-comment'>(* Prints "pi = 3.14159" *)</span>
The >> and << operator
The >>
and <<
operators perform function composition.
Given two functions f
and g
, f >> g
is equivalent to fn x => g(f(x))
.
The reverse operation f << g
is equivalent to fn x => f(g(x))
.