Signo.StdLib (Signo v0.0.2)
View SourceStandard Library for the Signo Programming Language.
A note on function names
Due to requirements for Elixir function names and conflicts with existing Elixir keywords and/or operators, the function names in this module are often different than the function names in Signo.
All functions have a small example along them, which contains the Signo names.
Summary
General
Pretty prints the given argument to stdout, returns the given argument.
Prints a string representation of the given argument
to stdout, and returns #ok
.
Operators
Boolean and
operator.
Boolean nand
operator.
Boolean not
operator.
Boolean or
operator.
Boolean xor
operator.
Equal to operator.
Greater-than operator.
Greater-than or equal to operator.
Less-than operator.
Less-than or equal to operator.
Not equal to operator.
Numbers
Adds two numbers.
Divides two numbers.
Multiplies two numbers.
Raise x
to the power n
, that is xⁿ
.
Substracts two numbers.
Math
Arithmetical absolute value of a
.
Inverse cosine of x
in radians.
Inverse sine of x
in radians.
Inverse tangent of x
in radians.
Cosine of x
in radians.
Euler's number.
Natural (base-e) logarithm of x
.
Base-10 logarithm of x
.
Base-n
logarithm of x
.
Ratio of the circumference of a circle to its diameter.
Sine of x
in radians.
Square root of x
.
Tangent of x
in radians.
Ratio of the circumference of a circle to its radius.
Strings
Converts the first character in the given string to uppercase and the remainder to lowercase.
Converts all characters in the given string to lowercase.
Returns a string where all leading and trailing Unicode whitespaces have been removed.
Converts all characters in the given string to uppercase.
Lists
Concatenates two lists or two strings.
Returns the first item of a list or the first Unicode grapheme in a string.
Joins the given list into a string with the second argument as seperator.
Returns the last item of a list or the last Unicode grapheme in a string.
Returns the amount of elements in a list of or the number of Unicode graphemes in a UTF-8 string.
Returns the element at index
in a list,
or the Unicode grapheme at index
in a string.
Returns a new list containing the first item of the old list and the remainder of the old list.
Returns the product of all numbers in a list.
Pushes the given item onto the end of a list.
Returns the sum of all numbers in a list.
Combines all passed arguments into a list.
REPL
Clears the console screen.
General
@spec inspect([Signo.AST.value()]) :: Signo.AST.Atom.t()
Pretty prints the given argument to stdout, returns the given argument.
sig> (inspect if)
<macro>(if)
<macro>(if)
@spec print([Signo.AST.value()]) :: Signo.AST.Atom.t()
Prints a string representation of the given argument
to stdout, and returns #ok
.
Only works for builtin types implementing String.Chars
:
- Strings
- Numbers
- Atoms
- Nil
Example
sig> (print 10)
10
#ok
Operators
@spec _and([Signo.AST.value()]) :: Signo.AST.Atom.t()
Boolean and
operator.
Receives two values (not limited to booleans), and returns #true
if both
are truthy. Does NOT short-circuit!
sig> (and 10 #true)
#true
sig> (and #false ())
#false
@spec _nor([Signo.AST.value()]) :: Signo.AST.Atom.t()
Boolean nand
operator.
Receives two values (not limited to booleans), and returns #true
if
both are falsy. Does NOT short-circuit!
sig> (nor () #true)
#false
sig> (nor #false ())
#true
@spec _not([Signo.AST.value()]) :: Signo.AST.Atom.t()
Boolean not
operator.
Receives any value (not limited to booleans) and returns #true
for falsy values,
and #false
for truthy ones.
sig> (not 10)
#false
sig> (not ())
#true
@spec _or([Signo.AST.value()]) :: Signo.AST.Atom.t()
Boolean or
operator.
Receives two values (not limited to booleans), and returns #true
if
one of them is truthy. Does NOT short-circuit!
sig> (or () #true)
#true
sig> (and #false ())
#false
@spec _xor([Signo.AST.value()]) :: Signo.AST.Atom.t()
Boolean xor
operator.
Receives two values (not limited to booleans), and returns #true
if
one of them is truthy, but returns #false
if both are truthy.
sig> (xor () #true)
#true
sig> (xor #false ())
#false
sig> (xor #true 10)
#false
@spec eq([Signo.AST.value()]) :: Signo.AST.Atom.t()
Equal to operator.
Returns #true
if the two terms are equal.
sig> (== "same" "same")
#true
sig> (== 1 1.0)
#true
sig> (== "not" "same")
#false
@spec gt([Signo.AST.Number.t()]) :: Signo.AST.Atom.t()
Greater-than operator.
Returns #true
if a
is greater than b
.
sig> (> 3 3)
#false
@spec gte([Signo.AST.Number.t()]) :: Signo.AST.Atom.t()
Greater-than or equal to operator.
Returns #true
if a
is greater than or equal to b
.
sig> (>= 3 3)
#true
@spec lt([Signo.AST.Number.t()]) :: Signo.AST.Atom.t()
Less-than operator.
Returns #true
if a
is less than b
.
sig> (< 3 3)
#false
@spec lte([Signo.AST.Number.t()]) :: Signo.AST.Atom.t()
Less-than or equal to operator.
Returns #true
if a
is less than or equal to b
.
sig> (<= 3 3)
#true
@spec not_eq([Signo.AST.value()]) :: Signo.AST.Atom.t()
Not equal to operator.
Returns #true
if the two terms are not equal.
sig> (!= "same" "same")
#false
sig> (!= 1 1.0)
#false
sig> (!= "not" "same")
#true
Numbers
@spec add([Signo.AST.Number.t()]) :: Signo.AST.Number.t()
Adds two numbers.
sig> (+ 2 3)
5
@spec div([Signo.AST.Number.t()]) :: Signo.AST.Number.t()
Divides two numbers.
sig> (/ 6 2)
3
@spec mult([Signo.AST.Number.t()]) :: Signo.AST.Number.t()
Multiplies two numbers.
sig> (* 2 3)
6
@spec pow([Signo.AST.Number.t()]) :: Signo.AST.Number.t()
Raise x
to the power n
, that is xⁿ
.
sig> (^ 2 3)
8
@spec sub([Signo.AST.Number.t()]) :: Signo.AST.Number.t()
Substracts two numbers.
sig> (- 3 2)
1
Math
@spec abs([Signo.AST.Number.t()]) :: Signo.AST.Number.t()
Arithmetical absolute value of a
.
sig> (abs -3)
3
sig> (abs 6)
6
@spec acos([Signo.AST.Number.t()]) :: Signo.AST.Number.t()
Inverse cosine of x
in radians.
sig> (acos -1)
3.14159...
@spec asin([Signo.AST.Number.t()]) :: Signo.AST.Number.t()
Inverse sine of x
in radians.
sig> (asin 0)
3.14159...
@spec atan([Signo.AST.Number.t()]) :: Signo.AST.Number.t()
Inverse tangent of x
in radians.
sig> (atan 0)
0
@spec cos([Signo.AST.Number.t()]) :: Signo.AST.Number.t()
Cosine of x
in radians.
sig> (cos (pi))
-1
@spec e([]) :: Signo.AST.Number.t()
Euler's number.
Floating point approximation of mathematical constant e.
sig> (e)
2.71828...
@spec ln([Signo.AST.Number.t()]) :: Signo.AST.Number.t()
Natural (base-e) logarithm of x
.
sig> (ln 1)
0
@spec log([Signo.AST.Number.t()]) :: Signo.AST.Number.t()
Base-10 logarithm of x
.
sig> (log 100)
2
@spec logn([Signo.AST.Number.t()]) :: Signo.AST.Number.t()
Base-n
logarithm of x
.
sig> (logn 2 8)
3
@spec pi([]) :: Signo.AST.Number.t()
Ratio of the circumference of a circle to its diameter.
Floating point approximation of mathematical constant π.
sig> (pi)
3.14159...
@spec sin([Signo.AST.Number.t()]) :: Signo.AST.Number.t()
Sine of x
in radians.
sig> (sin (pi))
0
@spec sqrt([Signo.AST.Number.t()]) :: Signo.AST.Number.t()
Square root of x
.
sig> (sqrt 4)
2
@spec tan([Signo.AST.Number.t()]) :: Signo.AST.Number.t()
Tangent of x
in radians.
sig> (tan (/ pi 4))
1
@spec tau([]) :: Signo.AST.Number.t()
Ratio of the circumference of a circle to its radius.
This constant is equivalent to a full turn when described in radians.
Same as (* 2 (pi))
.
sig> (tau)
6.28318...
Strings
@spec capitalize([Signo.AST.String.t()]) :: Signo.AST.String.t()
Converts the first character in the given string to uppercase and the remainder to lowercase.
sig> (capitalize "olá")
"Olá"
@spec downcase([Signo.AST.String.t()]) :: Signo.AST.String.t()
Converts all characters in the given string to lowercase.
sig> (downcase "HELLÖ")
"hellö"
@spec trim([Signo.AST.String.t()]) :: Signo.AST.String.t()
Returns a string where all leading and trailing Unicode whitespaces have been removed.
sig> (trim " signo ")
'signo'
@spec upcase([Signo.AST.String.t()]) :: Signo.AST.String.t()
Converts all characters in the given string to uppercase.
sig> (upcase "hellö")
"HELLÖ"
Lists
@spec concat([Signo.AST.List.t()]) :: Signo.AST.List.t()
@spec concat([Signo.AST.String.t()]) :: Signo.AST.String.t()
Concatenates two lists or two strings.
sig> (concat '(a b) '(c d))
(a b c d)
sig> (concat "hell" "o")
"hello"
@spec first([Signo.AST.List.t()]) :: Signo.AST.value()
@spec first([Signo.AST.String.t()]) :: Signo.AST.String.t() | Signo.AST.Nil.t()
Returns the first item of a list or the first Unicode grapheme in a string.
sig> (first ("hell" "o"))
"hell"
@spec join([Signo.AST.List.t() | Signo.AST.String.t()]) :: Signo.AST.String.t()
Joins the given list into a string with the second argument as seperator.
sig> (join '(2 3 4) ", ")
"2, 3, 4"
@spec last([Signo.AST.List.t()]) :: Signo.AST.value()
@spec last([Signo.AST.String.t()]) :: Signo.AST.String.t()
Returns the last item of a list or the last Unicode grapheme in a string.
sig> (last '(1 2 3))
3
sig> (last ())
()
sig> (last "hellö")
"ö"
sig> (last "")
()
@spec length([Signo.AST.List.t()]) :: Signo.AST.Number.t()
@spec length([Signo.AST.String.t()]) :: Signo.AST.Number.t()
Returns the amount of elements in a list of or the number of Unicode graphemes in a UTF-8 string.
sig> (length '(1 2 3))
3
sig> (length "hellö")
5
@spec nth([Signo.AST.Number.t() | Signo.AST.List.t()]) :: Signo.AST.value()
@spec nth([Signo.AST.String.t()]) :: Signo.AST.String.t()
Returns the element at index
in a list,
or the Unicode grapheme at index
in a string.
sig> (nth 1 '(1 2 3))
2
sig> (nth 3 '(1 2 3))
()
sig> (nth 4 "hellö")
"ö"
sig> (nth 5 "hellö")
()
@spec pop([Signo.AST.List.t()]) :: Signo.AST.List.t()
@spec pop([Signo.AST.String.t()]) :: Signo.AST.List.t()
Returns a new list containing the first item of the old list and the remainder of the old list.
sig> (pop '("hell" "o" "world"))
("hell" ("o" "world"))
sig> (pop ())
((), ()))
sig> (pop "hello")
("h", "ello"))
sig> (pop "")
((), ""))
@spec product([Signo.AST.List.t()]) :: Signo.AST.Number.t()
Returns the product of all numbers in a list.
Raises Signo.TypeError
if one of the elements of
the list is not a number.
sig> (product '(2 3 4))
24
@spec push([Signo.AST.value() | Signo.AST.List.t()]) :: Signo.AST.List.t()
Pushes the given item onto the end of a list.
Look out: this function only accepts lists. To
concatinate strings, use concat/2
.
sig> (push 3 '(1 2))
(1 2 3)
@spec sum([Signo.AST.List.t()]) :: Signo.AST.Number.t()
Returns the sum of all numbers in a list.
Raises Signo.TypeError
if one of the elements of
the list is not a number.
sig> (sum '(1 2 3))
6
@spec tie([Signo.AST.value()]) :: Signo.AST.List.t()
Combines all passed arguments into a list.
This function differs from creating a list by quoting in that here all arguments get evaluated before forming a list, where by quoting, the entire list remaings unevaluated:
sig> '(1 2 (+ 1 2))
(1 2 (+ 1 2))
sig> (tie 1 2 (+ 1 2))
(1 2 3)
REPL
@spec clear([]) :: Signo.AST.Atom.t()
Clears the console screen.
This function only works if ANSI escape codes are enabled on the shell, which means this function is by default unavailable on Windows machines.