Built-in Properties
Built-in properties are special values or functions provided for each type.
They can be accessed using the .<name>
notation, similar to object properties. ([<str>]
notation cannot be used.)
// Example
'ai kawaii'.len // 9
Core:range(0,2).push(4) // [0,1,2,4]
Currently, built-in properties are available for numbers, strings, arrays, and error types. For objects, their equivalents are implemented as standard functions for compatibility with the syntax.
Format
In this document, built-in properties for any value of a specific type are described in the following format:
#(v: type).property_name
or
@(v: type).built_in_method_name(argument_list): return_type
Entries starting with #
represent non-function values of built-in properties.
Entries starting with @
represent function-based built-in properties (built-in methods).
Numbers
@(x: num).to_str(): str
Converts a number to a string.
let x = 123
<: x.to_str()
@(x: num).to_hex(): str
Generates a hexadecimal string representation of the number.
let x = 123
<: x.to_hex()
Strings
#(v: str).len
Type: num
Gets the length of the string.
let x = "Hello World!"
<: x.len
@(v: str).to_num(): num | null
Converts the string to a number if it represents a numeric value.
let x = "123"
<: x.to_num()
let y = "abc"
<: y.to_num()
@(v: str).to_arr(): arr<str>
Splits the string into an array of grapheme clusters.
If the string contains no isolated surrogates, they are not returned.
@(v: str).to_unicode_arr(): arr<str>
Splits the string into an array of Unicode code points.
Grapheme clusters are divided.
If the string contains no isolated surrogates, they are not returned.
@(v: str).to_unicode_codepoint_arr(): arr<num>
Splits the string into Unicode code points and returns their numeric values as an array.
If the string contains no isolated surrogates, they are not returned.
@(v: str).to_char_arr(): arr<str>
Splits the string into an array of UTF-16 code units.
If the string contains surrogate pairs, both the high and low surrogates are returned separately.
@(v: str).to_charcode_arr(): arr<num>
Splits the string into UTF-16 code units and returns their numeric values as an array.
If the string contains surrogate pairs, both the high and low surrogates are returned separately.
@(v: str).to_utf8_byte_arr(): arr<num>
Encodes the string into UTF-8 and returns an array of byte values (0–255).
@(v: str).pick(i: num): str | null
Retrieves the i-th character in the string.
let x = "Hello World!"
<: x.pick(6)
@(v: str).incl(keyword: str): bool
Returns true
if the string contains keyword; otherwise, returns false
.
let x = "Hello World!"
<: x.incl("World")
let y = "こんにちは!"
<: y.incl("Hello")
@(v: str).starts_with(prefix: str, start_index?: num): bool
Returns true
if the string starts with prefix; otherwise, returns false
.
If prefix is an empty string, always returns true
.
If start_index is specified, starts checking from that index. If start_index exceeds the bounds of the string, returns false
.
let x = "Hello World!"
<: x.starts_with("Hello")
<: x.starts_with("World", 6)
@(v: str).ends_with(suffix: str, end_index?: num): bool
Returns true
if the string ends with suffix; otherwise, returns false
.
If suffix is an empty string, always returns true
.
If end_index is specified, considers the character just before end_index as the end of the string.
let x = "Hello World!"
<: x.ends_with("World!")
<: x.ends_with("Hello", 5)
@(v: str).slice(begin: num, end: num): str
Extracts a portion of the string from begin to end (exclusive).
let x = "Hello World!"
<: x.slice(6, 11)
@(v: str).split(splitter?: str): arr<str>
Splits the string into an array based on the delimiter splitter. If splitter is omitted, splits the string into individual characters.
let x = "Hey, how are you?"
<: x.split()
<: x.split(",")
Here is the English translation of the provided document while preserving the original formatting:
Arrays
#(v: arr).len
Type: num
Gets the number of elements in the array.
let x = [1, 2, 3, 4, 5]
<: x.len
@(v: arr).at(index: num, otherwise?: value): value
Returns the element at the position index in the array.
If index is negative, it counts from the end.
If index is out of range, it returns otherwise instead.
If otherwise is omitted, it defaults to null
.
let x = [1, 2, 3, 4, 5]
<: x.at(2)
<: x.at(-1)
<: x.at(5)
<: x.at(5, "Not Found")
@(v: arr).push(i: value): null
【This operation mutates the array】
Adds an element to the end of the array.
let x = [1, 2, 3, 4, 5]
x.push(8)
<: x
@(v: arr).unshift(i: value): null
【This operation mutates the array】
Adds an element to the beginning of the array.
let x = [1, 2, 3, 4, 5]
x.unshift(7)
<: x
@(v: arr).pop(): value
【This operation mutates the array】
Removes and returns the last element of the array.
let x = [1, 2, 3, 4, 5]
let popped = x.pop()
<: popped
<: x
@(v: arr).shift(): value
【This operation mutates the array】
Removes and returns the first element of the array.
let x = [1, 2, 3, 4, 5]
let shifted = x.shift()
<: shifted
<: x
@(a: arr).concat(b: arr): arr
Concatenates two arrays.
let x = [1, 2, 3]
let y = [4, 5, 6]
<: x.concat(y)
// The original arrays remain unchanged
<: x
<: y
@(v: arr<str>).join(joiner?: str): str
Joins the elements of a string array into a single string.
let x = ["Hello", "World", "!"]
<: x.join(" ")
// If the argument is omitted, elements are joined directly
<: x.join()
@(v: arr).slice(begin: num, end: num): arr
Extracts a section of the array from begin to end.
let x = [1, 2, 3, 4, 5]
<: x.slice(1, 4)
@(v: arr).incl(i: value): bool
Returns whether the specified value is present in the array.
let x = [1, 2, 3, 4, 5]
<: x.incl(3)
<: x.incl(6)
@(v: arr).map(func: fn): arr
Asynchronously calls func on each element of the array.
Returns a new array with elements replaced by the results of func.
let x = ['Tanaka', 'Suzuki', 'Yamamoto']
<: x.map(@(v) {
return `{v}-san`
})
@(v: arr).filter(func: fn): arr
Extracts elements from the array where func returns true.
The order is preserved.
let x = [1, 2, 3, 4, 5]
<: x.filter(@(v) {
// Extract even numbers
return v % 2 == 0
})
@(v: arr).reduce(func: Callback, initial: value): value
Callback
: @(acm: value, item: value, index: num): value
Calls func for each element of the array in turn.
In each call, the previous result is passed as the first argument acm.
If initial is specified, the argument of the first call is (initial, v[0], 0),
If initial is not specified, then (v[0], v[1], 1).
If the array is empty and initial is not specified, an error occurs. Therefore, it is basically recommended that initial be specified.
let x = [1, 2, 3, 4, 5]
<: x.reduce(@(acm, v) {
// Get sum
return acm + v
}, 0)
@(v: arr).find(func: @(item: value, index: num) { bool }): value
Find the first element in the array such that func returns true and returns their values.
let x = [1, 2, 3, 4, 5]
<: x.find(@(v) {
// 3より大きい最初の要素を探す
return v > 3
})
@(v: arr).index_of(val: value, fromIndex?: num): num
Searches the array for a value equal to val and returns its index.
If fromIndex is specified, the search starts at that position.
If fromIndex is negative, the position from the end (array length + fromIndex) is used.
If there is no match, -1 is returned.
let x = [1, 2, 3, 4, 5]
<: x.index_of(3)
@(v: arr).reverse(): null
【This operation mutates the array】
Reverses the array.
let x = [1, 2, 3, 4, 5]
x.reverse()
<: x
@(v: arr).copy(): arr
Generates a copy of the array.
It is a shallow copy, and array and object references are preserved.
let x = [1, 2, 3, 4, 5]
let xCopy = x.copy()
// Rewriting an array operation...
xCopy.push(6)
// ...does not affect the original array
<: x
<: xCopy
@(v: arr).sort(comp: @(a: value, b: value)): arr
【This operation mutates the array】
Sort an array. Pass the following comparison function as the first argument comp.
This operation is stable sort.
- returns a negative value if a is before b in order
- returns a positive value if a is later than b in order
- returns 0 when a is orderly equivalent to b * returns positive when a is orderly later than b * returns zero when a is orderly equal to b
For numeric sorting, you can pass Core:sub
for ascending order, or @(a,b){b-a}
for descending order.
Comparison functions for strings are provided as Str:lt
(ascending order) and Str:gt
(descending order). See std.md for details.
let x = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
x.sort(Core:sub)
/*
Alternatively...
x.sort(@(a, b) {
return a - b
})
*/
<: x
@(v: arr).fill(val?: value, fromIndex?: num, toIndex?: num): arr
【This operation mutates the array】
Replaces elements in the range fromIndex to toIndex of the array with val.
If val is omitted, it is replaced with null
.
The behavior regarding fromIndex and toIndex conforms to arr.slice
.
let x = [1, 2, 3, 4, 5]
// Fill the 1st through the 4th with 0.
x.fill(0, 1, 4)
<: x
@(v: arr).repeat(times: num): arr
Creates an array repeated times times.
Like arr.copy
, it is a shallow copy, and array and object references are preserved.
times must be an integer value greater than or equal to 0. Otherwise, throws error.
let x = [1, 2, 3]
<: x.repeat(3)
@(v: arr).splice(index: num, remove_count?: num, items?: arr<value>): arr<value>
【This operation mutates the array】
Removes remove_count elements from the index array and inserts items elements in their place.
Returns an array of the elements removed as the return value. If index is negative, count from the end.
If index is after the last element, no element is removed and the insertion is appended to the end.
If remove_count is omitted, removes up to the end.
If items is omitted, nothing is inserted.
let x = [1, 2, 3, 4, 5]
// Remove two from the second and insert [6, 7, 8] in its place
let spliced = x.splice(2, 2, [6, 7, 8])
<: spliced // Array of what was removed
<: x // Array after being removed and inserted
@(v: arr).flat(depth?: num): arr
Creates a new array that combines the arrays contained in the array to the depth hierarchy specified by depth.
The depth must be an integer value greater than or equal to 0. If omitted, it is set to 1.
let x = [1, [2, 3], [4, [5, 6]]]
<: x.flat()
<: x.flat(2)
@(v: arr).flat_map(func: @(item: value, index: num) { value }): arr
After replacing each element of the array with the return value of func, a new array is created, flattened by one level.
func is called asynchronously.
let x = [1, 2, 3]
<: x.flat_map(@(v) {
return [v, v * 2]
})
@(v: arr).insert(index: num, item: value): null
【This operation mutates the array】
Inserts item at the index position in the array.
If index is negative, count from the end.
If index is after the last element, appends it to the end.
let x = [1, 2, 3, 4, 5]
// Insert 6 in the third
x.insert(2, 6)
<: x
@(v: arr).remove(index: num): value | null
【This operation mutates the array】
Removes the element at position index from the array and returns that element.
If index is negative, count from the end.
If index is after the last element, it is not removed and null
is returned.
let x = [1, 2, 6, 3, 4, 5]
// Remove the third element
let removed = x.remove(2)
<: removed
<: x
@(v: arr).every(func: @(item: value, index: num) { bool }): bool
Returns true only if func returns true for all elements of the array. Always returns true for an empty array.
let x = [2, 4, 6, 8, 10]
let y = [2, 4, 6, 7, 8]
@judgeAllEven(arr) {
if (arr.every(@(v) { return v % 2 == 0 })) {
return "All array elements are even"
} else {
return "Some elements are odd"
}
}
<: judgeAllEven(x)
<: judgeAllEven(y)
@(v: arr).some(func: @(item: value, index: num) { bool }): bool
Returns true only when there is an element for which func returns true for an array element.
let x = [2, 4, 6, 7, 8]
@judgeHasOdd(arr) {
if (arr.some(@(v) { return v % 2 == 1 })) {
return "Array contains odd numbers"
} else {
return "All elements of the array are even numbers"
}
}
<: judgeHasOdd(x)
Error
#(v: error).name
Type: str
.
Get the string that is the identifier of the error.
#(v: error).info
Type: value
.
Get additional information about the error, if any.