AutolangDocs

Sets

A Set is an unordered collection of unique elements. It is highly optimized for fast membership testing — no duplicate values are allowed.

Creating Sets

Initialize a Set using the explicit constructor or using the syntactic sugar. Any duplicate values are automatically ignored.

// 1. Explicit Constructor val explicitSet = Set<Int>() explicitSet.add(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()}") // 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.

fun processGroups(data: Set<Set<Int>>) { println("Total groups: ${data.size()}") } 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.add("Orange") fruits.add("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 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()) // e.g. {1, 2, 3, 4, 5, 6} // Keeps only elements present in both sets val intersectSet = setA.intersect(setB) println(intersectSet.toString()) // {3, 4} // Keeps elements in setA that are NOT in setB val diffSet = setA.difference(setB) println(diffSet.toString()) // {1, 2}

Iteration & Conversion

Iterate over elements using forEach with a closure. Convert the set to an Array if you need indexed access.

val s = <Int>{10, 20, 30} // Single-expression closure s.forEach {|value| println("Set item: " + value) } // Block-body closure var sum = 0 s.forEach {|value| -> { sum = sum + value }} println("Sum of elements: " + sum) // Convert to an Array val arr = s.toArray() println("Array size: ${arr.size()}")

Methods Reference

Modification

  • add(value: T) — Adds a value. If it already exists, nothing happens.
  • remove(value: T) — Removes the specified value.
  • clear() — Removes all elements.

Querying

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

Mathematical Operations

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

Utilities

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