Built-in Functions
Essential¶
print¶
print AnyType;
- Prints any valid value without newline.
- No curved brackets.
Example
1 2 3 4 5 |
|
println¶
println AnyType;
- Prints any valid value with newline.
- No curved brackets.
Example
1 2 3 4 5 6 |
|
bai¶
bai AnyType;
- Exits Ari prematurely and prints out the given value.
- No curved brackets.
Example
1 2 3 |
|
Random generation ¶
random_choose¶
random_choose(source: Array, number_of_elements: Number) ➟ Array
- A uniform distribution is used to randomly select an element from the source.
- The number of elements determine the length of the generated array and must be zero or positive.
Example
- This example simulates 1000 throws of a fair dice:
1 2
println random_choose([1, 2, 3, 4, 5, 6], 1000); // 1000 random values // [2, 3, 4, 5, 2, ...]
random_normal¶
random_normal(mean: Number, standard_deviation: Number, number_of_elements: Number) ➟ Array
- A normal distribution is used to generate an array of random values using the mean and standard_deviation.
- The number of elements determine the length of the generated array and must be zero or positive.
Example
- This example generates 1000 samples with zero mean and a standard deviation of 1:
1 2
println random_normal(0, 1, 1000); // 1000 random values // [0.3446016, -0.2162096, -0.8018565, 0.60348547, 0.42210117, ...]
File operations ¶
read_file¶
read_file(file_path: String) ➟ Null
Example
- This example introduces a simple error-handling pattern in case the file is not found:
1 2 3 4 5 6 7
let contents = read_file("myfile.txt"); if (contents == null) { println "Failed to read file!"; } else { println "Success!"; }
write_file¶
write_file(file_path: String, data: String) ➟ Number
Example
- This example introduces a simple error-handling pattern in case the write operation fails:
1 2 3 4 5 6 7
let result = write_file("myfile.txt", "My passwords are ..."); if (result == 0) { println "Failed to write to file!"; } else { println "Success!"; }
Web stuff ¶
serve_static_folder¶
serve_static_folder(folder_path: String, address: String, port: Number) ➟ Nothing
- Starts a local web server which serves static files inside the given folder path.
- The most convenient address is "localhost" and the port must be a Number.
- Similar to how Express.js does it. More info here.
- Press Control-C to quit the server.
Example
- This example starts a server at localhost:8000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
serve_static_folder("public", "localhost", 8000); // Configured for staging. // => address: localhost // => port: 8000 // => log: normal // => workers: 16 // => secret key: generated // => limits: forms = 32KiB // => keep-alive: 5s // => read timeout: 5s // => write timeout: 5s // => tls: disabled // Mounting /: // => GET / [10] // => GET /<path..> [10] // Rocket has launched from http://localhost:8000
web_get¶
- Returns the response of a GET request from the given url.
- Returns Null if the request fails.
Example
- This example sends a GET request to this website and writes the response to a new HTML file:
1 2
let content = web_get("http://www.blankwebsite.com/"); write_file("blank.html", content);
web_post¶
web_get(url: String, parameters: Array) ➟ String
- Returns the response of a POST request from the given url.
- parameters must be an array of Strings). The elements with even-numbered indexes (0, 2, 4, ...) are regarded as keys, whereas elements with odd-numbered indexes (1, 3, 5, ...) are regarded as values.
- Returns Null if the request fails.
Example
-
This example sends a POST request to this website with two parameters and writes the response to a
response.txt
file:1 2
let content = web_post("http://httpbin.org/post", ["key1", "value1", "key2", "value2"]); write_file("response.txt", content);
-
The contents of
response.txt
looks like this:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
{ "args": {}, "data": "{\"key1\":\"value1\",\"key2\":\"value2\"}", "files": {}, "form": {}, "headers": { "Accept": "*/*", "Content-Length": "33", "Content-Type": "application/json", "Host": "httpbin.org", "X-Amzn-Trace-Id": "Root=1-6013ad21-1cf269b238720911742db09c" }, "json": { "key1": "value1", "key2": "value2" }, "origin": "219.95.127.175", "url": "http://httpbin.org/post" }
-
Lines 14 and 15 confirms that the website's server received our POST request successfully. ```