Skip to content

The Core Standard Library

C272 edited this page Dec 18, 2019 · 12 revisions

The contents of this library are automatically imported into Algo on runtime. You can call all of these without using any import statements. Don't try and import this library, as it will throw an error.


terminate(exitCode)

Safely terminates the running Algo program, given an exit code. '0' is considered a successful execution of the program, '-1' is considered a failure. This follows C# exit code guidelines.

Parameters:

Name Description
exitCode The exit code to assign to the program upon termination.

Usage:

//kill the program off.
terminate(0);

len(x)

Returns the length of the string or list passed in.

Parameters:

Name Description
x The item to return the length of.

Usage:

let x = [1, 2, 3];
print len(x); //3

let y = "string";
print len(y); //6

The string library

The entire string library is automatically imported in core. To see documentation for string manipulation, click here.


str(x)

Cast the given variable to a string, and returns it. If the cast fails, then an error is thrown.

Parameters:

Name Description
x The variable to cast to string.

Usage:

let x = 40; //40
let y = str(x); //"40"

int(x)

Cast the given variable to an integer, and returns it. If the cast fails, then an error is thrown.

Parameters:

Name Description
x The variable to cast to integer.

Usage:

let x = "40"; //40
let y = int(x); //40

rat(x)

Cast the given variable to a rational, and return it. If the cast fails, then an error is thrown.

Parameters:

Name Description
x The variable to cast to rational.

Usage:

let x = "40 / 10";
let y = rat(x); // 40/10

flt(x)

Cast the given variable to a float, and returns it. If the cast fails, then an error is thrown.

Parameters:

Name Description
x The variable to cast to float.

Usage:

let x = "40.2323";
let y = flt(x); //40.2323

hex(x)

Cast the given variable to hexadecimal, and returns it. If the cast fails, then an error is thrown.

Parameters:

Name Description
x The variable to cast to hex.

Usage:

let x = "40.2323";
let y = hex(x); //0x....

bool(x)

Cast the given variable to a bool, and return it. If the cast fails, then an error is thrown.

Parameters:

Name Description
x The variable to cast to bool.

Usage:

let x = "false";
let y = bool(x); //false

typeof(x)

Gets the type of a variable, and returns it. The type returned is a core.types enum.

Parameters:

Name Description
x The variable to get the type of.

Usage:

let x = 3; //integer
let typeOfX = typeof(x);

if (typeOfX == core.types.Integer)
{
    print "this is an integer!"; //this will run.
}

core.types

An enum, listing all the possible types in Algo. Returned from typeof(x).

Possible Values:

String,
Integer,
Float,
Rational,
Boolean,
List,
Function,
EmulatedFunction,
Object,
Break,
Continue,
Null

array.indexOf(item, arr)

Returns the index of a given item in an array. If the item is not present, -1 is returned.

Parameters:

Name Description
item The item to search for in the array.
arr The array to search.

Usage:

let x = [2, 3, 4];
let twoIndex = array.indexOf(2, x); //0

array.contains(item, arr)

Returns whether the given array contains an item.

Parameters:

Name Description
item The item to search for in the array.
arr The array to search.

Usage:

let x = [2, 3, 4];
let xHasTwo = array.contains(2, x);