AutolangDocs

Sets

A Set is an unordered collection of unique elements. It is highly optimized for fast membership testing and ensuring that no duplicate values exist within the collection.

Creating Sets

You can initialize a Set using the explicit constructor or by utilizing the clean syntactic sugar. Any duplicate values provided during initialization or insertion are automatically ignored.

// 1. Explicit Constructor val explicitSet = Set<Int>() explicitSet.insert(100) // 2. Sugar Syntax (Duplicates are ignored) val numbers = <Int>{1, 2, 3, 4, 5, 5, 4, 3, 2, 1} println("Size is: ${numbers.size()}") // Outputs: 5 println("Is empty? ${if (numbers.isEmpty()) "Yes" else "No"}")

Nested Sets

The curly brace syntax also supports nested sets. Type inference makes passing these structures into functions incredibly concise.

func processGroups(data: Set<Set<Int>>) { println("Total groups: ${data.size()}") } // The third set {1, 2} is a duplicate and will be ignored processGroups({{1, 2}, {3, 4}, {1, 2}})

Modifying & Querying

Sets provide efficient methods to check for element existence, add new items, and remove specific values.

val fruits = <String>{"Apple", "Banana"} fruits.insert("Orange") fruits.insert("Apple") // Ignored, already exists println("Has Orange? ${if (fruits.contains("Orange")) "Yes" else "No"}") println("Has Apple? ${if ("Apple" in fruits) "Yes" else "No"}") fruits.remove("Banana") fruits.clear() // Removes all elements

Set Operations

Autolang natively supports standard mathematical set operations: Union, Intersection, and Difference. These methods return a brand new Set.

val setA = <Int>{1, 2, 3, 4} val setB = <Int>{3, 4, 5, 6} // Combines elements from both sets val unionSet = setA.union(setB) println(unionSet.toString()) // Result: {4, 3, 2, 1, 6, 5} // Keeps only elements present in both sets val intersectSet = setA.intersect(setB) // Result: {4, 3} println(intersectSet.toString()) // Keeps elements in setA that are NOT in setB val diffSet = setA.difference(setB) // Result: {2, 1} println(diffSet.toString())

Iteration & Conversion

You can iterate over the elements using forEach by passing a named callback function. If you need indexed access or array-specific features, you can easily convert the set into an Array.

val s = <Int>{10, 20, 30} // Define a callback function func printValue(value: Int) { println(value) } // Iterate using the callback s.forEach(printValue) // Convert to an Array val arr = s.toArray() println("Array size: ${arr.size()}")

Methods Reference

Modification

  • insert(value: T) - Adds a value to the set. If the value already exists, nothing happens.
  • remove(value: T) - Removes the specified value from the set.
  • clear() - Removes all elements from the set.

Querying

  • contains(value: T): Bool - Returns true if the set contains the specified value.
  • size(): Int - Returns the number of elements in the set.
  • isEmpty(): Bool - Returns true if the set contains zero elements.

Mathematical Operations

  • union(other: Set<T>): Set<T> - Returns a new Set containing all unique elements from both sets.
  • intersect(other: Set<T>): Set<T> - Returns a new Set containing only elements found in both sets.
  • difference(other: Set<T>): Set<T> - Returns a new Set containing elements from the current set that are not in the other set.

Utilities

  • forEach(fn: (T) -> Void) - Executes a callback function once for each element.
  • toArray(): Array<T> - Converts the Set into a new Array containing the same elements.
  • toString(): String - Returns a string representation of the set.