AutolangDocs
core/set

Set

Set represents a collection of unique, unordered elements optimized for fast membership testing. Duplicate elements are automatically eliminated during initialization or insertion.

Initialization & Membership

Initialize sets using setOf(), mutableSetOf(), array conversion with toSet(), or the explicit constructor Set<T>(). Test membership quickly using the in and !in operators.

// Initialize with automatic deduplication val allowedRoles = setOf("ADMIN", "OPERATOR", "AUDITOR", "ADMIN") println("Allowed roles count: " + allowedRoles.size) // 3 // Convert array to set val uniqueTags = ["billing", "tax", "billing"].toSet() println("Unique tags count: " + uniqueTags.size) // 2 // Test membership with in and !in operators val roleToVerify = "OPERATOR" if (roleToVerify in allowedRoles) { println("Access verified for: " + roleToVerify) } println("Guest not in roles: " + ("GUEST" !in allowedRoles))

Set Algebra Operations

Autolang supports union, intersection, difference, and superset checks via standard methods or intuitive operators such as + and -.

val primaryTags = setOf("billing", "cloud", "security") val extraTags = setOf("security", "audit", "compliance") // Union via (+) operator val allTags = primaryTags + extraTags println("Union size: " + allTags.size) // 5 // Remove element via (-) operator val activeTags = allTags - "cloud" println("Cloud removed: " + ("cloud" !in activeTags)) // true // Intersection and difference between two sets val common = primaryTags.intersect(extraTags) // {"security"} val exclusive = primaryTags.difference(extraTags) // {"billing", "cloud"} println("Common tags count: " + common.size) // 1 // Superset check (issuperset) val baseSet = [1, 2, 3, 4].toSet() val subSet = [2, 3].toSet() println("Base is superset: " + baseSet.issuperset(subSet)) // true println("Subset is superset: " + subSet.issuperset(baseSet)) // false

Functional Transformations

Filter elements, compute totals, and transform sets cleanly using higher-order methods, or convert them back to indexed arrays when ordering is required.

val accountIds = setOf(101, 102, 205, 310) // Filter set elements val tierTwoAccounts = accountIds.filter { it >= 200 } println("Filtered accounts count: " + tierTwoAccounts.size) // 2 // Weighted sum calculation val weightedSum = accountIds.sumOf { it * 10 } println("Weighted sum: " + weightedSum) // 7180 // Convert to indexed array val accountList = accountIds.toArray() println("Array length: " + accountList.size)

Method Reference

Management & Inspection

  • size: Int / size(): Int: Number of unique elements in the set.
  • contains(element: T): Bool: Checks whether the element belongs to the set.
  • add(element: T): Bool: Adds an element to the set (returns false if already present).
  • remove(element: T): Bool: Removes an element from the set.
  • clear(): Removes all elements from the set.
  • issuperset(other: Set<T>): Bool: Checks if the current set contains all elements of the other set.
  • toArray(): Array<T>: Converts all elements of the set to an array.

Set Operations

  • operator +(other: Set<T>): Set<T>: Union of two sets.
  • operator -(element: T): Set<T>: Removes an element from the set.
  • intersect(other: Set<T>): Set<T>: Returns the intersection of two sets.
  • difference(other: Set<T>): Set<T>: Returns the difference between the current set and another set.