Foundation

Table of contents

  1. Global objects
  2. Global functions
  3. String (data type)
  4. List (data type)
  5. Map (data type)
  6. Function (data type)
  7. Iterable (data type)

Global objects

Bool, Int, Long, Float, Complex, List, Map, String, Range, Function, Iterable
The basic data types.
Type
The type of data types.

Global functions

print(x1,...,xn)
Print the arguments to the command-line interface. The output representations of the arguments are joined together without any separating character or spacing. Does print a line break thereafter. If the function is called with no argument, it only prints a line break.
put(x1,...,xn)
Does the same as print, but does not print a line break.
input(), input(prompt), input(prompt,history)
Read a line of input from the command-line interface and return the input as a string. The line break at the end of the input line is not contained in the string. If the POSIX terminal interface is accessible, use history (a list of strings) as a history to choose from.
str(x)
Convert x into a string.
str(x,format,precision)
Convert the floating point number x into a string.
# format = "[+|-](f|e|E|g|G)"
s = str(math.pi,"f",4)
int(x)
Convert x into an integer if possible.
float(x)
Convert x into a floating point number if possible.
hex(x)
Hexadecimal representation of an integer. Hexadecimal representation of binary data.
bin(x)
Binary representation of an integer.
ord(s)
Take a string of length one and return the code point of the Unicode character.
chr(x)
Take an integer value x and return the Unicode character at code point x. Return null if x is out of domain. A default value may be specified by null coalescing operation:
c = chr(x) else '?'
list(x)
Convert the iterable object x into a list.
> list(1..4)
[1, 2, 3, 4]
map(x)
Convert the iterable object x of [key,value] pairs into a map.
> map([["a",1],["b",2]])
{"a": 1, "b": 2}
set(x)
Turn an iterable object x into a set.
> set(1..4)
{1, 2, 3, 4}
iter(x)
Take an iterable object and return an iterator.
> i = iter(1..)
> [i(),i(),i(),i()]
[1, 2, 3, 4]
cycle(x)
Take an iterable object and return a cycling iterator.
> cycle(1..4).list(10)
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2]
len(a)
Number of elements of a. The object a should be a data structure that has this property.
rand(a..b), rand(a..b,seed)
Return a random number generator that returns random integers from a inclusive to b inclusive. Currently the generator is not cryptographically secure.
> r = rand(1..4)
> [r(),r(),r(),r()]
[2, 1, 3, 3]

> r.list(10)
[1, 1, 4, 2, 3, 4, 1, 2, 1, 1]
rand(a), rand(a,seed)
Return a random number generator that chooses an element of the list a randomly.
rand(), rand(seed)
Return a random number generator that returns floats between zero and one.
read(id), read(id,mode)
Read the text file with filename id and encoding UTF-8. Return the contents as an UTF32-string. If mode=='b', the file is read as a binary file.
load(id)
Load the module with filename id and return the module object. The function load can be used in a more general way than import statements (keyword use). One can achieve dynamic loading this way.
> math = load("math")
> math.floor(2.4)
2.0
abs(x)
Absolute value of x. Returns an integer if x is an integer. Takes also complex numbers.
sgn(x)
Sign of x. Returns an integer if x is an integer.
max(a,b)
Return the maximum of a and b.
min(a,b)
Return the minimum of a and b.
pow(a,n,m)
Modular exponentiation: calculate (a^n)%m fast.
const(x), const(n,x)
Shallow freeze a mutable object x and return it. If n is given, deep freeze x by depth n. If n is null, freeze the complete tree. Note that const(x) is equivalent to const(1,x).
copy(x), copy(n,x)
Construct a shallow copy of the object x and return it. If n is given, return a deep copy by depth n. If n is null, return a complete deep copy. Note that copy(x) is equivalent to copy(1,x).
type(x)
Prototype of x.
record(x)
Slot table of x.
getattr(x,key)
The same as x.(key), but returns null instead of an exception, in case the property was not found. Usage pattern:
y = getattr(x,key) else default_value
extend(a,b)
Insert all slots of b into the slot table of a. Already existent slots of a will not be overwritten.
zip(a1,...,an)
Take the iterables a1,...,an and return an iterator that produces lists [y1,...,yn] for yk in ak until one of the iterators is exhausted.
> zip("abcd",[1,2,3,4]).list()
[["a", 1], ["b", 2], ["c", 3], ["d", 4]]

> zip("abcd",1..).list()
[["a", 1], ["b", 2], ["c", 3], ["d", 4]]

> zip(1..,1..).map(|[x,y]| x*y).list(10)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

> (1..).map(|x| x*x).list(10)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Note that, for a list of lists, zip is an involution:
a == zip(*zip(*a)).list()
eval(s), eval(s,m)
Execute a string as a program. Free variables are from the current environment (only global variables). If the map m is given, use it as an environment for free/global variables.
> x=12
> eval("2*x")
24

> eval("a=[1,2]")
> a
[1, 2]

> eval("x*y",{x=360,y=240})
86400

String (Iterable)

s.lower()
Return the string s projected to lower case.
> "Bee".lower()
"bee"
s.upper()
Return the string s projected to upper case.
> "Bee".upper()
"BEE"
s.islower()
Return true if the string s is empty or all characters are lower case letters.
s.isupper()
Return true if the string s is empty or all characters are upper case letters.
s.isalpha()
Return true if the string s is empty or consists only of letters.
s.isdigit(), s.isdigit(radix)
Return true if the string s is empty or consists only of digits.
s.isalnum()
Return true if the string s is empty or each character is a letter or a digit.
s.ltrim(), s.ltrim(chars)
Remove withespace from the left side. If a string chars is given, remove characters until a character is found, that is not contained in chars.
s.rtrim(), s.rtrim(chars)
Remove withespace from the right side.
s.trim(), s.trim(chars)
Remove withespace from the left and right side.
s.encode(spec="UTF-8")
Encode s by encoding specification spec.

List (Iterable)

a.push(x)
Append x to a.
a.pop()
Remove the last element of a and return it.
a.pop(i)
Remove the element at index i from a and return it. Thus, a.pop(0) removes the first element.
a.insert(i,x)
Insert the object x at index i. Beforehand, all elements from index i inclusive onwards are shiftet one position to the right.
a.append(b), a.push(*b)
Append all elements from b to a.
a.clear(), a.clear(n)
Remove all elements. Remove all elements, but keep the first n.
a.map(f)
Create a shallow copy of a, apply the function f to every element of this copy and return the result.
> [1,2,3,4].map(|x| 2*x)
[2, 4, 6, 8]
a.filter(p)
Filter all elements from a for which the predicate p is true.
> [1,2,3,4].filter(|x| x%2==0)
[2, 4]
a.rev()
Reverse a and return it. Does not create a shallow copy, a itself will be modified.
> list(1..4).rev()
[4, 3, 2, 1]
a.rot(n)
Rotate a by n positions. If n>0, rotate to the right side. If n<0, rotate to the left side.
a.shuffle()
Shuffle a randomly and return it. Does not create a shallow copy, a itself will be modified.
> list(1..10).shuffle()
[10, 9, 5, 3, 1, 7, 6, 2, 8, 4]
a.chain()
Construct a new list with the same elements, but if elements of a are lists or ranges, these will be unpacked.
> [[1,2],[3,4]].chain()
[1, 2, 3, 4]

> [[1,2],11..14,3,4].chain()
[1, 2, 11, 12, 13, 14, 3, 4]

Map (Iterable)

m.values()
Return an iterator of the values.
m.items()
Return an iterator of the key-value pairs.
m.update(m2)
Insert all items of m2 into m. Already in m contained items are overwritten.
m.extend(m2)
Insert all items of m2 into m, but do not overwrite already in m contained items.
m.add(key)
Add a key to the map.
m.remove(key)
Remove an item from the map and return it. Raise an exception if the key is not found.
m.clear()
Remove all items of m.

Function (Iterable)

f.orbit(x)
Return an iterator that produces [x,f(x),f(f(x)),...].
> (|x| 2*x).orbit(2).list(10)
[2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
f.argc()
Return the function's argument count. If the function is variadic, return a..b instead of a number. If the function is variadic without upper bound, return (a..).

Iterable

a.all(p), a.all()
Universal quantifier: Return only true if the predicate p is true for all elements of a.
> [1,2,3,4].all(|x| x%2==0)
false
a.any(p), a.any()
Existential quantifier: Return true if the predicate p is true for at least one element of a.
> [1,2,3,4].any(|x| x%2==0)
true
a.count(p), a.count()
Apply predicate p to every element of a and count how often it is true. If no predicate is given, the total number of elements will be returned.
> [1,2,3,4].count(|x| x%2==0)
2
a.until(p)
Return a new iterator that takes elements from a as long as the predicate p is false.
isprime = |n| n>1 and (2..).until(|i| i*i>n).all(|i| n%i!=0)
a.sum(f), a.sum()
Calculate the sum of all f(x) for x in a.
a.prod(f), a.prod()
Calculate the product of all f(x) for x in a.
a.reduce(f), a.reduce(e,f)
Reduce the iterable a from left to right by the binary function f. That means, if f is regarded as a left associative binary operator, this operator is inserted between all elements of a. If e is given, it is used as initial element.
# 1+2+3+...+99+100
> (1..100).reduce(|x,y| x+y)
5050

> (1..4).reduce("#",|x,y| x+"/"+str(y))
"#/1/2/3/4"

> fac = |n| (1..n).reduce(1,|x,y| x*y)
> fac(6)
720
a.map(f)
Apply the function f to every element of a and return a new iterator.
> (1..).map(|x| x^2).list(10)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
a.filter(p)
Filter all elements from a for which the predicate p is true and return a new iterator.
> isprime = |n| (1..n).count(|k| n%k==0)==2
> (1..).filter(isprime).list(10)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
a.max(), a.max(p)
Return the maximum of a. Return the maximum based on a projection p.
a.min(), a.min(p)
Return the minimum of a. Return the minimum based on a projection p.
> a = [[1,"dca"],[2,"b"],[3,"ab"]]
> a.min(|t| t[0])
[1, "dca"]

> a.min(|t| t[1])
[3, "ab"]

> a.min(|t| len(t[1]))
[2, "b"]
a.join(sep="",left="",right="")
Apply str to each element and join the strings together.
> (1..4).join()
"1234"
If sep is given, it will be inserted between.
> (1..4).join(", ")
"1, 2, 3, 4"
The result will be surrounded by left and right, if given.
> (1..4).join(", ","(",")")
"(1, 2, 3, 4)"
a.enum(), a.enum(start)
Return an iterator that enumerates the elements of a.
> "abcd".enum().list()
[[0, "a"], [1, "b"], [2, "c"], [3, "d"]]

> "abcd".enum(1).list()
[[1, "a"], [2, "b"], [3, "c"], [4, "d"]]
a.list(), a.list(n)
Take the iterable a and return its elements as a list. If n is given, a maximum number of n elements will be taken. This is equivalent to a.take(n).list().
> (1..).list(4)
[1, 2, 3, 4]
a.take(n)
Return a new iterator that takes a maximum number of n elements from the iterable a.
a.skip(n)
Leave out the next n elements of the iterable a.
a.chunks(n)
Return a new iterator with the same elements, but the elements are grouped to chunks of size n.
> (1..6).chunks(2).list()
[[1, 2], [3, 4], [5, 6]]
a.sort(), a.sort(p), a.sort(p,cmp)
Sort the iterable and return it. If a is a list, it does not create a shallow copy, thus a itself will be modified. The function p is a projection on which the sorting is based, also known as key function. The function cmp is an alternative binary comparison function. The sorting algorithm is not required to be stable, and cannot be, as cmp does not return information about the equality case.
> a = ["oak", "Elm", "willow", "birch"]
> a.sort()
["Elm", "birch", "oak", "willow"]

> a.sort(|x| x.lower())
["birch", "Elm", "oak", "willow"]

> a.sort(len)
["oak", "Elm", "birch", "willow"]

> a.sort(null,|x,y| x>y)
["willow", "oak", "birch", "Elm"]