Working with Lists and Maps in Soul
// Empty list empty = [] // List with values numbers = [1, 2, 3, 4, 5] mixed = [1, "hello", true, null, [1, 2]] // Lists can span multiple lines users = [ "Alice", "Bob", "Charlie" ]
fruits = ["apple", "banana", "orange"] first = fruits[0] // "apple" last = fruits[2] // "orange" // Negative indices count from the end lastItem = fruits[-1] // "orange" secondLast = fruits[-2] // "banana"
list = [1, 2, 3] // Add elements list.push(4) // Adds to end: [1, 2, 3, 4] list.unshift(0) // Adds to beginning: [0, 1, 2, 3, 4] // Remove elements last = list.pop() // Removes & returns last: 4 first = list.shift() // Removes & returns first: 0 // Get information length = list.length() // 3 isEmpty = list.isEmpty() // false
items = ["a", "b", "c"] // For-in loop for (item in items) { print(item) } // With index for (i in 0..items.length()-1) { print(i + ": " + items[i]) } // forEach method items.forEach(soul(item, index) { print(index + ": " + item) })
// Empty map empty = {} // Map with values person = { "name": "Alice", "age": 30, "active": true } // Nested maps config = { "database": { "host": "localhost", "port": 5432 }, "cache": { "enabled": true, "ttl": 3600 } }
user = { "name": "Bob", "email": "bob@example.com", "age": 25 } // Bracket notation name = user["name"] // Dot notation email = user.email // Nested access db = config.database.host // or db = config["database"]["host"]
data = {"a": 1, "b": 2} // Get all keys and values keys = data.keys() // ["a", "b"] values = data.values() // [1, 2] // Check existence hasKey = data.has("a") // true // Add/update values data["c"] = 3 data.d = 4 // Remove values removed = data.remove("a") // Returns removed value // Get with default value = data.get("missing", "default") // "default" // Merge maps merged = data.merge({"e": 5, "f": 6})
scores = { "Alice": 95, "Bob": 87, "Charlie": 92 } // Iterate over keys for (name in scores) { print(name + ": " + scores[name]) } // Iterate over key-value pairs for (name, score in scores) { print(name + " scored " + score) }
// List destructuring coordinates = [10, 20] [x, y] = coordinates // Map destructuring in loops users = [ {"name": "Alice", "age": 30}, {"name": "Bob", "age": 25} ] for (user in users) { print(user.name + " is " + user.age) }
// Combining lists list1 = [1, 2, 3] list2 = [4, 5, 6] combined = [...list1, ...list2] // [1, 2, 3, 4, 5, 6] // Copying lists original = [1, 2, 3] copy = [...original] // Map spreading defaults = {"theme": "dark", "lang": "en"} userPrefs = {"lang": "es"} settings = {...defaults, ...userPrefs} // Result: {"theme": "dark", "lang": "es"}
// Sort a list numbers = [3, 1, 4, 1, 5] sorted = numbers.sort() // [1, 1, 3, 4, 5] // Custom sort people = [ {"name": "Charlie", "age": 35}, {"name": "Alice", "age": 30}, {"name": "Bob", "age": 25} ] byAge = people.sort(soul(a, b) { return a.age - b.age }) // Reverse a list reversed = [1, 2, 3].reverse() // [3, 2, 1] // Join list elements words = ["Hello", "Soul", "World"] sentence = words.join(" ") // "Hello Soul World"
// Instead of multiple pushes list = [] list.reserve(1000) // Pre-allocate
Was this page helpful?