Skip to content

Standard Library

Luís Carlos Moreira edited this page Mar 2, 2018 · 16 revisions

String

String.count(String) -> Integer
Returns the number of UTF-8 codepoints in the string.

String.count("address") # 7

String.first(String) -> String
Returns the first character of the string.

String.first("profession") # p

String.last(String) -> String
Returns the last character of the string.

String.last("user") /* r */

String.lower(String) -> String
Converts all the characters of the string to lowercase.

String.lower("Age") # age

String.upper(String) -> String
Converts all the characters of the string to uppercase.

String.upper("date") # DATE

String.capitalize(String) -> String
Capitalize the string to title case. All the first characters of words will be uppercase.

String.capitalize("luis costa") // "Luis Costa"

String.reverse(String) -> String
Reverses the characters of the string.

String.reverse("roma") // amor
String.reverse("Oro Language") // "egaugnaL orO"

String.slice(String, start Integer, length Integer) -> String
Returns a substring from start with length.

String.slice("hello", 2, 3) # llo

String.trim(String, subset String) -> String
Removes all the characters from subset from the start and end of the string.

"[" + String.trim("  hello world  ", " ") + "]" // [ hello world ]
String.trim("!@LuisCM@!", "!@") // "LuisCM"

String.trimLeft(String, subset String) -> String
Removes all the characters from subset from the start of the string.

String.trimLeft("  hello world  hello world", " ") // [ hello world  hello world]

String.trimRight(String, subset String) -> String
Removes all the characters from subset from the end of the string.

String.trimRight("  hello world  ", " ") // [  hello world ]

String.join(Array, sep String) -> String
Joins an array with sep into a string.

String.join(["luis", "moreira"], " carlos ") // "luis carlos moreira"

String.split(String, sep String) -> Array
Splits a string by sep into an array.

String.split("hello world", " ") // ["hello", "world"]

String.starts?(String, prefix String) -> Boolean
Checks if the string starts with prefix.

String.starts?("abc", "a") # true

String.ends?(String, suffix String) -> Boolean
Checks if the string ends with suffix.

String.ends?("abc", "c") # true

String.contains?(String, search String) -> Boolean
Checks if the string contains search.

String.contains?("hello", "ll") // true
String.contains?("a2b3c5", "b") // true

String.replace(String, search String, replace String) -> String
Replaces replace with search in the string.

String.replace("hello", "ll", "LL") // "heLLo"

String.match?(String, regex String) -> Boolean
Checks if the string matches a PCRE-compatible regular expression.

String.match?("123", "[a-z]+") // true

Math

Math.pi -> Float
Returns the constant pi.

Math.pi # 3.141593

Math.e -> Float
Returns the constant e.

Math.e # 2.718282

Math.floor(Float) -> Integer
Rounds the number to the closest low integer.

Math.floor(12.7) // 12

Math.ceil(Float) -> Integer
Rounds the number to the closest high integer.

Math.ceil(24.5) // 25

Math.max(Float|Integer, Float|Integer) -> Float|Integer
Returns the biggest of the two numbers.

Math.max(33, 22) # 33

Math.min(Float|Integer, Float|Integer) -> Float|Integer
Returns the smallest of the two numbers.

Math.min(104, 73) # 73

Math.random(min Integer, max Integer) -> Integer
Generates a pseudo random number between min and max.
Not a cryptographically pseudo random number.

Math.random(50, 200) // 197

Math.abs(Float|Integer) -> Float|Integer
Returns the absolute value of the number.

Math.abs(-69) # 69

Math.pow(Float|Integer, exponent Float|Integer) -> Float|Integer
Returns the exponentiation of the number with exponent.

Math.pow(256, 2) # 65536

Enum

Enum.size(Array) -> Integer
Returns the number of elements in the enumerable.

Enum.size(1..10) # 10

Enum.empty?(Array) -> Boolean
Checks if the enumerable is empty.

Enum.empty?(1..10) # false
Enum.empty?([]) # true

Enum.reverse(Array) -> Array
Reverses the elements of the enumerable.

Enum.reverse([1, 2, 3]) // [3, 2, 1]
Enum.reverse(1..10) // [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Enum.first(Array) -> Any
Returns the first element of the array.

Enum.first(1..5) # 1
Enum.first(["abc", "q", "wrere"]) # abc

Enum.last(Array) -> Any
Returns the last element of the array.

Enum.last(1..10) # 10
Enum.last(["a", "b", "c"]) # c

Enum.insert(Array, element Any) -> Array
Insert element at the end of the array.

Enum.insert([1, 2], 3) // [1, 2, 3]
Enum.insert(1..5, 6) // [1, 2, 3, 4, 5, 6]

Enum.delete(Array, index) -> Array
Delete an element at index.

Enum.delete(1..5, 3) # [1, 2, 3, 5]

Enum.map(Array, fun Function(element)) -> Array
Maps function fun to every element of the array.
The element parameter represents the array element in iteration.

Enum.map([1, 2, 3], (x) -> x * x) // [1, 4, 9]
Enum.map(1..10, (x) -> x * 2) // [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Enum.filter(Array, fun Function(element)) -> Array
Keeps every element of the array for which the function fun returns true,
element is the array element in iteration.

Enum.filter(1..10, (x) -> x % 2 == 1) // [1, 3, 5, 7, 9]

Enum.reduce(Array, start Any, fun Function(element, accumulator)) -> Any
Maps function fun to every element of the array, accumulating the start value,
element is the array element in iteration and accumulator the accumulated value.

Enum.reduce([1, 2, 3], 0, (x, acc) -> x + acc) // 6

val calc = fn mult, ...nums
  mult * Enum.reduce(nums, 0, fn x, acc do x + acc end)
end
print("reduce ")
println(calc(10, 1, 2, 3, 4)) # 100

val calc2 = fn mult, ...nums
  mult * Enum.reduce(nums, 0, (x, acc) -> x + acc)
end

print("reduce ")
println(calc2(10, 1, 2, 3, 4)) // 100

Enum.find(Array, fun Function(element)) -> Any
Returns the first element for which the function fun returns true.
This is a shortcut to running: Enum.first(Enum.filter(...)) element is the array element in iteration.

Enum.find("a".."z", (x) -> x == "b") // b
Enum.find(1..10, (x) -> x % 2 == 0) // 2

Enum.contains?(Array, search Any) -> Boolean
Checks if the enumerable contains an element search.

Enum.contains?(["hello", "world"], "hello") // true
Enum.contains?(1..10, 6) // true

Enum.unique(Array) -> Array
Removes all duplicate values from the array.

Enum.unique([0, 1, 1, 2, 3]) // [0, 1, 2, 3]
Enum.unique([10, 1, 9, 2, 3, 4, 5, 6, 4, 2, 9, 10]) // [10, 1, 9, 2, 3, 4, 5, 6]

Enum.random(Array) -> Any
Returns a random element from the array.
The function that calculates the random index is not cryptographically secure, if that matters.

Enum.random(1..100)
Enum.random(100..1)

Dictionary

Dictionary.size(Dictionary) -> Integer
Returns the number of elements in a dictionary.

val user = ["name" => "Luis", "profession" => "Developer", "age" => 50]
Dictionary.size(user) # 3

Dictionary.contains?(Dictionary, key String) -> Boolean
Checks if the dictionary contains a key.

Dictionary.contains?(user, "age") # true

Dictionary.empty?(Dictionary) -> Boolean
Checks if the dictionary is empty.

Dictionary.empty?(user)

Dictionary.insert(Dictionary, key String, value Any) -> Dictionary
Inserts a key with value into the dictionary.

Dictionary.insert(["name" => "Carlos", "age" => 50], "single", false) # age => 50, single => false, name => Carlos]
Dictionary.insert(user, "address", "St One, 100") # [profession => Developer, age => 50, address => St One, 100, name => Luis]

Dictionary.update(Dictionary, key String, value Any) -> Dictionary
Update a key with value on the dictionary.

Dictionary.update(["name" => "Carlos", "age" => 50], "name", "Alberto") # [name => Alberto, age => 50]
Dictionary.update(user, "address", "St Two, 60") # [profession => Developer, age => 50, address => St Two, 60, name => Luis]

Dictionary.delete(Dictionary, key) -> Dictionary
Deletes the key from the dictionary.

Dictionary.delete(["name" => "Carlos", "age" => 50], "age") # [name => Carlos]
Dictionary.delete(user, "address") # [age => 50, name => Luis, profession => Developer]

Type

Type.of(Any) -> String
Returns the type as String of the value.

Type.of(2001) // Integer

Type.isNumber?(Any) -> Boolean
Checks if the type is an Integer or Float.

Type.isNumber?(12) // true

Type.toString(Any) -> String
Tries to convert the value to String.

Type.toString(57) // 57

Type.toInteger(Any) -> Integer
Tries to convert the value to integer.

Type.toInteger(74) // 74 

Type.toFloat(Any) -> Float
Tries to convert the value to Float.

Type.toFloat(90.67) // 90.670000

Type.toArray(Any) -> Array Tries to convert the value to a single element array.

Type.toArray(80.12) // [80.120000]

Clone this wiki locally