std

Contains common functions

Constants

ARGS : string desktop = command-line arguments

OwnLang : map = {PLATFORM=android/desktop, VERSION=2.0.1_000000, VERSION_MAJOR=2, VERSION_MINOR=0, VERSION_PATCH=1}

Functions

arrayCombine(keys, values) — creates map by combining two arrays

arrayKeyExists(key, map) — checks existing key in map. 1 - exists, 0 - no

arrayKeys(map) — returns array of map keys

arraySplice(array, start, deleteCount = length(array) - start, additions = []) — returns new array with removed deleteCount elements starting from start and/or added new elements from start index

arrayValues(map) — returns array of map values

charAt(input, index) — returns char code in position index of string input

clearConsole() android — clears console

default(a, b) 1.4.0 — returns value a if it it non empty, returns b otherwise

use std

user = {"name": "", "lastname": "Doe"}
name = default(user.name, "Unknown")
lastname = default(user.lastname, "Unknown")
println name + " " + lastname  // Unknown Doe

echo(arg...) — prints values to console, separate them by space and puts newline at the end. Takes variable number of arguments

use std

echo(1, "abc") // prints "1 abc" to console
echo(1, 2, 3, 4, 5, "a", "b") // prints "1 2 3 4 5 a b"

exit(status) 2.0.0 — terminates an application with provided status code. Non-zero values indicates abnormal termination

use std

println "Bye!"
exit(0)

getBytes(input, charset = "UTF-8") 1.5.0 — returns byte array of the string in the given charset

getenv(key, defaultValue = "") 2.0.0 — returns the value of the specified environment variable if it's exists, returns defaultValue otherwise

use std
println getenv("JAVA_HOME")

getprop(key, defaultValue = "") 2.0.0 — returns the value of the system property if it's exists, returns defaultValue otherwise

indexOf(input, what, index = 0) — finds first occurrence of what in string input, starting at position index

join(array, delimiter = "", prefix = "", suffix = "") — join array to string with delimiter, prefix and suffix

lastIndexOf(input, what, index = 0) — finds last occurrence of what in string input, starting at position index

length(x) — returns length of string, array/map size or number of function arguments

nanotime() 2.0.0 — returns VM time source in nanoseconds for elapsed time purposes

newarray(size...) — creates array with size. newarray(x) - creates 1D array, newarray(x,y) - creates 2D array

use std

println newarray(4) // [0, 0, 0, 0]
println newarray(2, 3) // [[0, 0, 0], [0, 0, 0]]

parseInt(str, radix) — parses the input string into an integer with radix base

parseLong(str, radix) — parses the input string into a long integer with radix base

parseDouble(str) 2.0.0 — parses the input string into a double

rand(from = 0, to = ..) — returns pseudo-random number.
rand() - returns float number from 0 to 1
rand(max) - returns random number from 0 to max
rand(from, to) - return random number from from to to

range(from = 0, to, step = 1) — creates lazy array by number range.
range(to) - creates range from 0 to to (exclusive) with step 1
range(from, to) - creates range from from to to (exclusive) with step 1
range(from, to, step) - creates range from from to to (exclusive) with step step

use std

println range(3) // [0, 1, 2]
r = range(-5, 0) // [-5, -4, -3, -2, -1]
println r[0] // -5
println r[2] // -3
for x : range(20, 9, -5) {
  println x
} // 20 15 10

readln(x) desktop — reads a line from console

replace(str, target, replacement) — replaces all occurrences of string target with string replacement

replaceAll(str, regex, replacement) — replaces all occurrences of regular expression regex with string replacement

replaceFirst(str, regex, replacement) — replaces first occurrence of regular expression regex with string replacement

sleep(time) — causes current thread to sleep for time milliseconds

sort(array, comparator = ..) — sorts array by natural order or by comparator function

split(str, regex, limit = 0) — splits string str with regular expression regex into array. limit parameter affects the length of resulting array

use std

println split("a5b5c5d5e", "5") // ["a", "b", "c", "d", "e"]
println split("a5b5c5d5e", "5", 3) // ["a", "b", "c5d5e"]

sprintf(format, args...) — formats string by arguments

stringFromBytes(input, charset = "UTF-8") 1.5.0 — returns a string from byte array in the given charset

stripMargin(input, marginPrefix = "|") 1.5.0 — trims leading whitespaces followed by marginPrefix on each line and removes the first and the last lines if they are blank

use std

println "
        |123
        |456
        ".stripMargin() // "123\n456"

substring(str, startIndex, endIndex = ..) — returns string from startIndex to endIndex or to end of string if endIndex is not set

use std

println substring("abcde", 1) // bcde
println substring("abcde", 2, 4) // cd

sync(callback) — calls an asynchronous function synchronously

use std, http

url = "https://whatthecommit.com/index.txt"
result = sync(def(ret) {
   http(url, def(t) = ret(t))
})
println result

thread(func, args...) — creates new thread with parameters if passed

use std

thread(def() {
  println "New Thread"
})

thread(::newthread, 10)
thread("newthread", 20)

def newthread(x) {
  println "New Thread. x = " + x
}

time() — returns current time in milliseconds from 01.01.1970

toChar(code) — converts char code to string

use std

println toChar(48) // "0"

toHexString(number) — converts number into hex string

toLowerCase(str) — converts all symbols to lower case

toUpperCase(str) — converts all symbols to upper case

trim(str) — removes any leading and trailing whitespaces in string

try(unsafeFunction, catchFunction = def(type, message) = -1) — suppress any error in unsafeFunction and returns the result of the catchFunction if any error occurs

use std

println try(def() = "success") // success
println try(def() = try + 2) // -1
println try(def() = try(), def(type, message) = sprintf("Error handled:\ntype: %s\nmessage: %s", type, message))
println try(def() = try(), 42) // 42