Signo.StdLib (Signo v0.0.2)

View Source

Standard 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

inspect(list)

@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)

print(list)

@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

_and(list)

@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

_nor(list)

@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

_not(list)

@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

_or(list)

@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

_xor(list)

@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

eq(list)

@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

gt(list)

Greater-than operator.

Returns #true if a is greater than b.

sig> (> 3 3)
#false

gte(list)

@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

lt(list)

Less-than operator.

Returns #true if a is less than b.

sig> (< 3 3)
#false

lte(list)

@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

not_eq(list)

@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

add(list)

Adds two numbers.

sig> (+ 2 3)
5

div(list)

Divides two numbers.

sig> (/ 6 2)
3

mult(list)

@spec mult([Signo.AST.Number.t()]) :: Signo.AST.Number.t()

Multiplies two numbers.

sig> (* 2 3)
6

pow(list)

Raise x to the power n, that is xⁿ.

sig> (^ 2 3)
8

sub(list)

Substracts two numbers.

sig> (- 3 2)
1

Math

abs(list)

Arithmetical absolute value of a.

sig> (abs -3)
3
sig> (abs 6)
6

acos(list)

@spec acos([Signo.AST.Number.t()]) :: Signo.AST.Number.t()

Inverse cosine of x in radians.

sig> (acos -1)
3.14159...

asin(list)

@spec asin([Signo.AST.Number.t()]) :: Signo.AST.Number.t()

Inverse sine of x in radians.

sig> (asin 0)
3.14159...

atan(list)

@spec atan([Signo.AST.Number.t()]) :: Signo.AST.Number.t()

Inverse tangent of x in radians.

sig> (atan 0)
0

cos(list)

Cosine of x in radians.

sig> (cos (pi))
-1

e(list)

@spec e([]) :: Signo.AST.Number.t()

Euler's number.

Floating point approximation of mathematical constant e.

sig> (e)
2.71828...

ln(list)

Natural (base-e) logarithm of x.

sig> (ln 1)
0

log(list)

Base-10 logarithm of x.

sig> (log 100)
2

logn(list)

@spec logn([Signo.AST.Number.t()]) :: Signo.AST.Number.t()

Base-n logarithm of x.

sig> (logn 2 8)
3

pi(list)

@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...

sin(list)

Sine of x in radians.

sig> (sin (pi))
0

sqrt(list)

@spec sqrt([Signo.AST.Number.t()]) :: Signo.AST.Number.t()

Square root of x.

sig> (sqrt 4)
2

tan(list)

Tangent of x in radians.

sig> (tan (/ pi 4))
1

tau(list)

@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

capitalize(list)

@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á"

downcase(list)

@spec downcase([Signo.AST.String.t()]) :: Signo.AST.String.t()

Converts all characters in the given string to lowercase.

sig> (downcase "HELLÖ")
"hellö"

trim(list)

@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'

upcase(list)

@spec upcase([Signo.AST.String.t()]) :: Signo.AST.String.t()

Converts all characters in the given string to uppercase.

sig> (upcase "hellö")
"HELLÖ"

Lists

concat(list)

@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"

first(list)

Returns the first item of a list or the first Unicode grapheme in a string.

sig> (first ("hell" "o"))
"hell"

join(list)

Joins the given list into a string with the second argument as seperator.

sig> (join '(2 3 4) ", ")
"2, 3, 4"

last(list)

@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 "")
()

length(list)

@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

nth(list)

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ö")
()

pop(list)

@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 "")
((), ""))

product(list)

@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

push(list)

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)

sum(list)

@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

tie(arguments)

@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

clear(list)

@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.