MufiZ Standard Library

Expand the world of possibilities!

Standard Library

The Mufi-Lang standard library provides a wide range of functionalities to facilitate the development of various applications. The library is designed to be user-friendly and efficient, offering a comprehensive set of tools to enhance the language's capabilities. The standard library is continuously evolving, with new features and improvements being added regularly to meet the diverse needs of developers.

Multi-types Names

Numeric: (Integer, Double, Complex)
Collection: (Array, Linked List, Hash Table, FVec, Matrix)
List: (Array, Linked List, FVec)

Math

log2(double) double

Return the base-2 logarithm of a floating-point number.

Example Usage:

var x = log2(8.0); // 3.0
log10(double) double

Return the base-10 logarithm of a floating-point number.

Example Usage:

var x = log10(100.0); // 2.0
sin(double) double

Return the sine of a floating-point number.

Example Usage:

var x = sin(0.0); // 0.0
cos(double) double

Return the cosine of a floating-point number.

Example Usage:

var x = cos(0.0); // 1.0
tan(double) double

Return the tangent of a floating-point number.

Example Usage:

var x = tan(0.0); // 0.0
asin(double) double

Return the arcsine of a floating-point number.

Example Usage:

var x = asin(0.0); // 0.0
acos(double) double

Return the arccosine of a floating-point number.

Example Usage:

var x = acos(1.0); // 0.0
atan(double) double

Return the arctangent of a floating-point number.

Example Usage:

var x = atan(0.0); // 0.0
sqrt(double) double

Return the square root of a floating-point number.

Example Usage:

var x = sqrt(4.0); // 2.0
pow(numeric, double) double

Return the value of a number raised to the power of another number.

Example Usage:

var x = pow(2.0, 3.0); // 8.0
abs(numeric) (double/int)

Return the absolute value of a number.

Example Usage:

var x = abs(-5); // 5
phase(complex) double

Return the phase of a complex number.

Example Usage:

var x = phase(complex(3.0, 4.0)); // 0.927295
ceil(double) int

Return the smallest integer greater than or equal to a floating-point number.

Example Usage:

var x = ceil(3.14); // 4
floor(double) int

Return the largest integer less than or equal to a floating-point number.

Example Usage:

var x = floor(3.14); // 3
round(double) int

Return the value of a floating-point number rounded to the nearest integer.

Example Usage:

var x = round(3.14); // 3
pi() double

Return the value of the mathematical constant π.

Example Usage:

var x = pi(); // 3.14159
sfc() int

Random number generator using Sfc64.

Example Usage:

var x = sfc(); // 1374538710
rand() double

Return a random floating-point number in the range [0, 1).

Example Usage:

var x = rand(); // 0.267054
randn() double

Return a random floating-point number from a normal distribution with mean 0 and standard deviation 1.

Example Usage:

var x = randn(); // 0.260098

Conversions

double(int) double

Convert an integer to a floating-point number.

Example Usage:

var x = double(5); // 5.0
int(double) int

Convert a floating-point number to an integer.

Example Usage:

var x = int(5.0); // 5
str(int/double) string

Convert an integer or double to a string.

Example Usage:

var x = string(5); // "5"

Time

now() int

Return the current time in seconds since the Unix epoch.

Example Usage:

var x = now(); // 1708896589
now_ns() double

Return the current time in nanoseconds since the Unix epoch.

Example Usage:

var x = now_ns(); // 1.7089e+18
now_ms() double

Return the current time in milliseconds since the Unix epoch.

Example Usage:

var x = now_ms(); // 1.7089e+12

File System

create_file(string) bool

Create a new file with the specified path.

Example Usage:

if(create_file("file.txt")){
    print "Created file `file.txt`";
} else {
    print "Failed to create file `file.txt`";
}
write_file(string, string) bool

Write to a file with the specified path and data.

Example Usage:

if(write_file("file.txt", "Hello World!")){
    print "Wrote to file `file.txt`";
} else {
    print "Failed to write to file `file.txt`";
}                   
read_file(string) string

Read from a file with the specified path.

Example Usage:

var str = read_file("file.txt"); // "Hello, World!"
delete_file(string) bool

Delete a file with the specified path.

Example Usage:

if(delete_file("file.txt")){
    print "Deleted file `file.txt`";
} else {
    print "Failed to delete file `file.txt`";
}
create_dir(string) bool

Create a new directory with the specified path.

Example Usage:

if(create_dir("files")){
    print "Created directory `files`"; 
} else {
    print "Failed to create directory `files`";
}
delete_dir(string) bool

Delete a directory with the specified path.

Example Usage:

if(delete_dir("files){
    print "Deleted directory `files`";
} else {
    print "Failed to delete directory `files`";
}

Collections

array(int) array

Create a dynamic array with an optional starting capacity or initialize an empty array.

Example Usage:

var a = array(); // []
linked_list() linked list

Create a new empty linked list.

Example Usage:

var ll = linked_list(); // []
hash_table() hash table

Create a new empty hash table.

Example Usage:

var ht = hash_table(); // {}
matrix(int, int) matrix

Create a matrix with the specified dimensions.

Example Usage:

var m = matrix(2, 2);
print m;
0 0
0 0
fvec(int/array) fvec

Create a float vector with size or an array of values.

Example Usage:

var v = fvec(3); // [0.0, 0.0, 0.0]
slice(list, int, int) list

Return a slice of a list from start to end.

Example Usage:

var a = array(3, true);
push(a, 1, 2, 3);
print slice(a, 0, 2);
[1, 2]
splice(list, int, int) list

Remove a slice of a list from start to end.

Example Usage:

var a = array(3, true);
push(a, 1, 2, 3);
print splice(a, 0, 2);
[1, 2]
print a;
[3]
push(list, any, ...)

Append elements to the end of a list.

Example Usage:

push(a, 1, 2, 3); // a = [1, 2, 3]
push_front(linked_list, any, ...)

Append elements to the front of a linked list.

Example Usage:

push_front(ll, 1, 2, 3); // ll = [3, 2, 1]
pop(list) any

Remove and return the last element of a list.

Example Usage:

var x = pop(a); // x = 3, a = [1, 2]
pop_front(linked_list) any

Remove and return the first element of a linked list.

Example Usage:

var x = pop_front(ll); // x = 3, ll = [2, 1]
nth(list, int) any

Get the value from a specified index in a list.

Example Usage:

var x = nth(a, 1); // x = 2
is_empty(collection) bool

Check if a collection type is empty.

Example Usage:

if(is_empty(a)){
    print "Array is empty";
} else {
    print "Array is not empty";
}
sort(list) nil

Sort a list (qsort for array, merge sort for linked list).

Example Usage:

var a2 = array();
push(a2, 3, 1, 2);
sort(a2); // a2 = [1, 2, 3]
reverse(list) nil

Reverse a list.

Example Usage:

reverse(a); // a = [3, 2, 1]
merge(list, list) list

Merge two lists into a single list.

Example Usage:

var a3 = merge(a, a2); // a3 = [1, 2, 3, 1, 2, 3]
put(table, string, any) nil

Insert a new entry into a hash table with a string key and any value.

Example Usage:

put(ht, 'key', 'value'); // ht = {'key': 'value'}
get(table, string) any

Get the value from a hash table with a specified key.

Example Usage:

var x = get(ht, 'key'); // x = 'value'
remove(table, string) nil

Remove an entry from a hash table with a specified key.

Example Usage:

remove(ht, 'key'); // ht = {}
equal_list(list, list) bool

Check if two lists are equivalent (must be the same list type), you can also use `==`.

Example Usage:

var a1 = array();
push(a1, 1, 2, 3);
var a2 = array();
push(a2, 1, 2, 3);
if(equal_list(a1, a2)){
    print "Lists are equivalent";
} else {
    print "Lists are not equivalent";
}
contains(collection, any) bool

Check if a collection type contains a specified value.

Example Usage:

if(contains(a, 1)){
    print "Array contains 1";
} else {
    print "Array does not contain 1";
}
len(collection) int

Return the count of a collection.

Example Usage:

var x = len(a); // x = 3
range(int, int) array

Return an array with range [a, b).

Example Usage:

var a = range(0, 5); // a = [0, 1, 2, 3, 4]
linspace(float, float, int) fvec

Return a float vector with n evenly spaced values between a and b.

Example Usage:

var v = linspace(0.0, 1.0, 5); // v = [0.0, 0.25, 0.5, 0.75, 1.0]
search(list, any) int

Search for an item in a list and return its index.

Example Usage:

var x = search(a, 2); // x = 1
interp1(fvec, fvec, float) float

Interpolate a value from two float vectors.

Example Usage:

var x = fvec(3);
var y = fvec(3);
push(x, 0.0, 1.0, 2.0);
push(y, 0.0, 1.0, 4.0);
var z = interp1(x, y, 1.5); // z = 2.5
sum(list) double

Return the sum of a list.

Example Usage:

var x = sum(a); // x = 6
mean(list) double

Return the mean of a list.

Example Usage:

var x = mean(a); // x = 2.0
var(list) double

Return the variance of a list.

Example Usage:

var x = var(a); // x = 1.0
std(list) double

Return the standard deviation of a list.

Example Usage:

var x = std(a); // x = 1.0
maxl(list) any

Return the maximum value of a list.

Example Usage:

var x = maxl(a); // x = 3
minl(list) any

Return the minimum value of a list.

Example Usage:

var x = minl(a); // x = 1
set_row(matrix, int, array) nil

Set a row in a matrix with an array.

Example Usage:

var m = matrix(2, 2);
var a = array();
push(a, 1, 2);
set_row(m, 0, a);
print m;
1 2
0 0
set_col(matrix, int, array) nil

Set a column in a matrix with an array.

Example Usage:

var m = matrix(2, 2);
var a = array();
push(a, 1, 2);
set_col(m, 0, a);
print m;
1 0
2 0
set(matrix, int, int, any) nil

Set a value in a matrix at a specified row and column.

Example Usage:

var m = matrix(2, 2);
set(m, 0, 0, 1);
print m;
1 0
0 0
kolasa() matrix

Create a Kolasa matrix.

Example Usage:

var m = kolasa();
print m;
1 2 3
4 5 6
7 8 9
rref(matrix) void

Return the reduced row echelon form of a matrix.

Example Usage:

var k = kolasa();
rref(k);
print k;
1 0 -1
0 1 2
0 0 0
rank(matrix) int

Return the rank of a matrix.

Example Usage:

var x = rank(k); // x = 2
det(matrix) float

Return the determinant of a matrix.

Example Usage:

var x = det(k); // x = 0.0
transpose(matrix) matrix

Return the transpose of a matrix.

Example Usage:

var k = kolasa();
var t = transpose(k);
print t;
1 4 7
2 5 8
3 6 9
lu(matrix) (matrix, matrix)

Return the LU decomposition of a matrix.

Example Usage:

var k = kolasa();
var lu_val = lu(k);
print nth(lu_val, 0, 0);
print nth(lu_val, 1, 1);

Networking

Requires enable_fs feature that is only available for Unix systems with cURL.

Content Type Integer Value
PlainText 0
HTML 1
JSON 2
XML 3
PDF 4
JPEG 5
PNG 6

The functions example usages are based off of testing with a simple webserver which is found here! They are not guaranteed to work with all web servers, that may require more complex content types, or other headers. These functions are built for simple requests and responses.

get_req(string, int, string, string) string

This functions sends a GET request to the given URL with the respective integer code for the ContentType and optional string parameters for User-Agent and Authorization headers. The function returns the response body as a string.

Example Usage:

var res = get_req("http://127.0.0.1:8000/data", 0);
print res;
// Output: 
Default
post_req(string, string, int, string, string) string

This functions sends a POST request to the given URL with the data to be written to it and the respective integer code for the ContentType and optional string parameters for User-Agent and Authorization headers. The function returns the response body as a string.

Example Usage:

var res = post_req("http://127.0.0.1:8000/pos_data", "new value from post", 0);
print res;
// Output:
Data updated to new value from post
put_req(string, string, int, string, string) string

This functions sends a PUT request to the given URL with the data to be written to it and the respective integer code for the ContentType and optional string parameters for User-Agent and Authorization headers. The function returns the response body as a string.

Example Usage:

var res = put_req("http://127.0.0.1:8000/put_data", "new value from put", 0);
print res;
// Output:
Data updated to new value from put
delete_req(string, int, string, string) string

This functions sends a DELETE request to the given URL with the respective integer code for the ContentType and optional string parameters for User-Agent and Authorization headers. The function returns the response body as a string.

Example Usage:

var res = delete_req("http://127.0.0.1:8000/del_data", 0);
print res;
// Output:
Data deleted