Skip to content

Primitive Methods

LabRicecat edited this page Nov 25, 2022 · 1 revision

You can not only call methods on objects, but also on some primitive types. These are:

  • String
  • List
  • Dictionary

Here is a list for all of them:

String

length

Returns the length of the string.

new my_string "Hello, World!"
my_string.length()
12

front

Returns the first character of the string.

new my_string "Hello, World!"
my_string.front()
"H"

back

Returns the last character of the string.

new my_string "Hello, World!"
my_string.back()
"!"

at

Returns the character of the string at a given index.

new my_string "Hello, World!"
my_string.at(4)
"o"

Errors if the string is empty.
Errors if the index is invalid.

insert

Inserts a character to a given position in the string.

new my_string "Hello, World!"
my_string.insert(12,"?")
print my_string
"Hello, World?!"

Errors if the string is empty.
Errors if the index is invalid.

replace

Replace a character in the string with a given sequence.

new my_string "Hello, World!"
my_string.replace(0,"h")
my_string.replace(12,"??")
print my_string
"hello, World??"

Errors if the string is empty.
Errors if the index is invalid.

erase

Erases a character at a given position in the string.

new my_string "Hello, World!"
my_string.erase(3)
print my_string
"Helo, World!"

Errors if the string is empty.
Errors if the index is invalid.

pop_front

Erases the first character of the string.

new my_string "Hello, World!"
my_string.pop_front()
print my_string
"ello, World!"

Errors if the string is empty.

pop_back

Erases the last character of the string.

new my_string "Hello, World!"
my_string.pop_back()
print my_string
"Hello, World"

Errors if the string is empty.

push_back

Adds a character to the end of the string.

new my_string "Hello, World!"
my_string.push_back("!")
print my_string
"Hello, World!!"

has

Returns 1 it the string contains the given character, 0 if else.

new my_string "Hello, World!"
my_string.has(",")
my_string.has("i")
1
0

count

Returns the amount of characters matching the given character.

new my_string "Hello, World!"
my_string.count("l")
my_string.count("i")
my_string.count("W")
2
0
1

empty

Returns 1 if the string is empty, 0 if else.

new my_string "Hello, World!"
my_string.empty()
set my_string ""
my_string.empty()
0
1

to_upper

Turns all letters in the string to their uppercase version.

new my_string "Hello, World!"
my_string.to_upper()
print my_string
HELLO, WORLD!

to_lower

Turns all letters in the string to their lowercase version.

new my_string "Hello, World!"
my_string.to_lower()
print my_string
hello, world!

List

length

Returns the length of the list.

new my_list [1,"2",[3,4]]
my_list.length()
3

front

Returns the first element of the list.

new my_list [1,"2",[3,4]]
my_list.front()
1

Errors if the list is empty.

back

Returns the last element of the list.

new my_list [1,"2",[3,4]]
my_list.front()
[3,4]

Errors if the list is empty.

at

Returns the element of the list at a given index.

new my_list [1,"2",[3,4]]
my_list.at(1)
"2"

Errors if the list is empty.
Errors if the index is invalid.

insert

Inserts a value to a given position in the list.

new my_list [1,"2",[3,4]]
my_list.insert(1,"my dear")
print my_list
[1,"my dear","2",[3,4]]

Errors if the list is empty.
Errors if the index is invalid.

replace

Replaces an element in the list with a given value.

new my_list [1,"2",[3,4]]
my_list.replace(1,"5")
print my_list
[1,"5",[3,4]]

Errors if the list is empty.
Errors if the index is invalid.

erase

Erases an element in the list.

new my_list [1,"2",[3,4]]
my_list.erase(0)
print my_list
["2",[3,4]]

Errors if the list is empty.
Errors if the index is invalid.

pop_front

Erases the first element in the list.

new my_list [1,"2",[3,4]]
my_list.pop_front()
print my_list
["2",[3,4]]

Errors if the list is empty.

pop_back

Erases the last element in the list.

new my_list [1,"2",[3,4]]
my_list.pop_back()
print my_list
[1,"2"]

Errors if the list is empty.

push_back

Adds a value to the end of the list.

new my_list [1,"2",[3,4]]
my_list.push_back(5)
print my_list
[1,"2",[3,4],5]

has

Returns 1 if the list has the given value and 0 if not.

new my_list [1,"2",[3,4]]
my_list.has(1)
my_list.has(12)
1
2

count

Returns the amount of elements matching the given value.

new my_list [1,"2",[3,4],"2"]
my_list.count("2")
my_list.count(1)
my_list.count(5)
1
2
0

empty

Returns 1 if there are no elements in the list and 0 if else.

new my_list [1,"2",[3,4]]
my_list.empty()
set my_list []
my_list.empty()
0
1

sort

Sorts the list.

new my_list ["ab",4,1,"ac","a",[4,5],[3]]
my_list.sort()
print my_list
[1,4,"a","ab","ac",[3],[4,5]]

It is sorted by:

  • Number
    -- small to big
  • String
    -- lowest size to greatest size
    -- lowest ascii value to greatest ascii value
  • List
    -- lowest size to greatest size

sorted

Returns a sorted version of the list.

new my_list ["ab",4,1,"ac","a",[4,5],[3]]
my_list.sorted()
print my_list
[1,4,"a","ab","ac",[3],[4,5]]
["ab",4,1,"ac","a",[4,5],[3]]

Dictionary

length

Returns the amount of key/value -pairs

new my_dic {
	a = 12,
	b = "hihi",
	c = 42
}
my_dic.length()
3

set

Sets a key to a value.

new my_dic {
	a = 12,
	b = "hihi",
	c = 42
}
my_dic.set(c,3)
my_dic.set(d,"huh")

print my_dic
{a = 12,b = "hihi",c = 3,d = "huh"}

get

Retruns a value based of a given key.

new my_dic {
	a = 12,
	b = "hihi",
	c = 42
}
my_dic.get(c)
42

keys

Returns a list of all keys.

new my_dic {
	a = 12,
	b = "hihi",
	7 = 42
}
my_dic.keys()
["a","b",7]

values

Returns a list of all values.

new my_dic {
	a = 12,
	b = "hihi",
	7 = 42
}
my_dic.values()
[12,"hihi",42]

>>> Next Article