Skip to content

Latest commit

 

History

History
144 lines (105 loc) · 4.5 KB

builtin-props.md

File metadata and controls

144 lines (105 loc) · 4.5 KB

A built-in property is a special value or function for specific type that can be called in the same notation as an object property.

// example
'ai kawaii'.len //9

Core:range(0,2).push(4) //[0,1,2,4]

Currently, built-in properties for types number, string, array, and error are available.
The object equivalent is implemented as std function due to confliction of notation.

Format

#(v: type_name).prop_name

Prefix # indicates it is a primitive property that is not a function.

@(v: type_name).prop_name(args): return_type

Prefix @ indicates it is a primitive property that is a function.

Number

@(x: num).to_str(): str

Gets string representation of the number.

String

#(v: str).len

type: num
Length of the string.

@(v: str).to_num(): num | null

Gets the numeric value that the string represents.

@(v: str).pick(index: num): str | null

Gets the character at index .

@(v: str).incl(keyword: str): bool

Checks for the presence of keyword in the string.

@(v: str).slice(begin: num, end: num): str

Obtains specified portion of the string.

@(v: str).split(splitter?: str): arr

Returns the string separated into array at the point where the splitter is located.

@(v: str).replace( old: str, new: str): str

Returns the string with including old (s) replaced with new.

@(v: str).index_of(search: str): num

Searches for search in the string and returns its index.

@(v: str).trim(): str

Returns the string with leading and trailing whitespace removed.

@(v: str).upper(): str

Returns the string converted to uppercase.

@(v: str).lower(): str

Returns the string converted to lowercase.

@(v: str).codepoint_at(index: num): num | null

Gets the codepoint of the character at index.
Returns null if the character does not exist there.

Array

#(v: arr).len

type: num
Number of elements in the array.

@(v: arr).push(i: value): null

MODIFIES THE ARRAY
Appends an element to the end of the array.

@(v: arr).unshift(i: value): null

MODIFIES THE ARRAY*
Prepends an element to the beginning of the array.

@(v: arr).pop(): value

MODIFIES THE ARRAY*
Extracts the last element of the array.

@(v: arr).shift(): value

MODIFIES THE ARRAY*
Extracts the first element of the array.

@(a: arr).concat(b: arr): arr

Returns an array concatenating the array and b.

@(v: arr).join(joiner?: str): str

Combines all elements of the string array and returns them as a single string.

@(v: arr).slice(begin: num, end: num): arr

Obtains specified portion of the array.

@(v: arr).incl(val: value): bool

Checks if there is an element in the array with the value val.

@(v: arr).map(func: fn): arr

Executes func for each element of the array asynchronously.
Returns the array of results.

@(v: arr).filter(func: fn): arr

Executes func for each element of the array asynchronously.
Returns only those elements of the array for which func returns true.

@(v: arr).reduce(func: @(acm: value, item: value, index: num): value, initial?: value): value

Executes func for each element in turn.
func is given the previous result as acm.
if initial is given, func is initially called with arguments (initial, v[0], 0).
Otherwise, (v[0], v[1], 1).

@(v: arr).find(func: @(item: value, index: num) { bool }): value

Finds elements in the array such that func returns true.

@(v: arr).index_of(val: value, fromIndex?: num): num

Finds a value that equals to val, and returns the index.
If fromIndex is given, the search starts from there. When fromIndex is negative, index from the end(length of the array + fromIndex) is used. When not found, returns -1.

@(v: arr).reverse(): null

Modifying Reverses the array.

@(v: arr).copy(): arr

Generates a copy of the array.

@(v: arr).sort(comp: @(a: value, b: value)): arr

MODIFIES THE ARRAY* Sorts the array.
comp is the comparison function that returns:

  • negative value if a should precede b
  • positive value if a should succeed b
  • 0 if either is acceptable

Str:lt and Str:gt are available as comparison function. See std.md

Error type

#(v: error).name

type: str
Identifier string of the error.

#(v: error).info

type: value
Additional information about the error, if any.