Language guide · 8 of 12
Built-in Collection Methods
Array Methods
arr := [1, 2, 3]
arr.len() // 3
arr.push(4) // mutates arr -> [1, 2, 3, 4] (alias: arr.append(4))
arr.pop() // returns 4, arr is now [1, 2, 3]
arr.slice(1, 3) // [2, 3] (supports negative indices; end is optional)
arr.contains(2) // true
["a", "b"].join("-") // "a-b" (elements must be strings)
arr.reverse() // returns new array [3, 2, 1] (does not mutate arr)
arr.map(fn(x) => x * 10) // [10, 20, 30]
arr.filter(fn(x) => x > 1)// [2, 3]Open in playgroundObject Methods
obj := { name: "John", age: 25 }
obj.keys() // ["name", "age"] (in the order the keys were added)
obj.values() // ["John", 25] (in the same order)
obj.len() // 2
obj.has("name") // true (alias: obj.contains("name"), or obj.name?)
obj.remove("age") // returns 25, removes key (alias: obj.delete("age"))
obj.clear() // removes all entriesOpen in playgroundString Methods
Positions and lengths count characters (UTF-8 runes), not bytes:
s := " Hello, Evie! "
s.len() // character (UTF-8 rune) count
s.trim() // "Hello, Evie!"
s.trimStart() // "Hello, Evie! "
s.trimEnd() // " Hello, Evie!"
"v1.2".trimPrefix("v") // "1.2"
"a.ev".trimSuffix(".ev") // "a"
s.toUpper() // " HELLO, EVIE! "
s.toLower() // " hello, evie! "
s.contains("Evie") // true
s.startsWith(" He") // true
s.endsWith("! ") // true
"héllo".index("l") // 2 (-1 if not found)
"hello".lastIndex("l") // 3 (-1 if not found)
"banana".count("a") // 3
"a-b-c".replace("-", "+") // "a+b+c"
"a-b-c".replaceFirst("-", "+") // "a+b-c"
"ab".repeat(3) // "ababab"
"héllo".reverse() // "olléh"
"7".padStart(3, "0") // "007"
"ab".padEnd(5, "xy") // "abxyx"
"a,b,c".split(",") // ["a", "b", "c"]
" a b c ".fields() // ["a", "b", "c"]
"x\ny\n".lines() // ["x", "y"]
"héy".chars() // ["h", "é", "y"]
"café".slice(1, 4) // "afé" (also s[1:4])Open in playgroundBuffer Methods
buf := buffer("hi") // also buffer(), buffer(size), buffer([65, 66]), buffer(other)
file := fs.readFile("test.txt")
buf.len() // byte length (2)
buf[0] // 104 (byte value 0..255)
buf[0] = 72 // mutate byte in place ("Hi")
buf.push("!") // append byte (0..255), string, or buffer (alias: buf.append)
buf.slice(0, 2) // new buffer copy (also buf[0:2])
buf.string() // "Hi!"
string(buf) // "Hi!" (string conversion constructor)Open in playground