CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-scala-lang--scala3-library-sjs1-3

Scala.js-specific runtime library components for Scala 3, providing JavaScript-specific functionality and bridge components between Scala 3 and the Scala.js runtime environment

Pending
Overview
Eval results
Files

collections.mddocs/

Immutable Collections

High-performance immutable arrays with covariant types and comprehensive collection operations, providing JavaScript-optimized implementations of standard collection patterns.

Capabilities

IArray - Immutable Arrays

Covariant immutable arrays with complete collection API and primitive specializations.

/**
 * Immutable array with covariant type parameter
 * Provides O(1) access with immutable semantics
 */
opaque type IArray[+T] = Array[? <: T]

object IArray:
  /** Create empty IArray */
  def empty[T]: IArray[T]
  
  /** Create IArray from elements */
  def apply[T](elems: T*): IArray[T]
  
  /** Create IArray with repeated element */
  def fill[T](n: Int)(elem: => T): IArray[T]
  
  /** Create IArray using index function */
  def tabulate[T](n: Int)(f: Int => T): IArray[T]
  
  /** Create IArray with range of integers */  
  def range(start: Int, end: Int): IArray[Int]
  def range(start: Int, end: Int, step: Int): IArray[Int]
  
  /** Create IArray from iterable */
  def from[T](it: IterableOnce[T]): IArray[T]
  
  /** Concatenate multiple IArrays */
  def concat[T](arrays: IArray[T]*): IArray[T]

/**
 * Extension methods for IArray instances
 */
extension [T](arr: IArray[T])
  // Access operations
  /** Get element at index */
  def apply(index: Int): T
  /** Get array length */
  def length: Int
  /** Check if array is empty */
  def isEmpty: Boolean
  /** Check if array is non-empty */
  def nonEmpty: Boolean
  /** Get indices range */
  def indices: Range
  
  // Element access
  /** Get first element (unsafe) */
  def head: T
  /** Get first element safely */
  def headOption: Option[T]
  /** Get last element (unsafe) */
  def last: T
  /** Get last element safely */
  def lastOption: Option[T]
  /** Get all elements except first */
  def tail: IArray[T]
  /** Get all elements except last */
  def init: IArray[T]
  
  // Transformation operations
  /** Transform each element */
  def map[U](f: T => U): IArray[U]
  /** Transform with index */
  def mapWithIndex[U](f: (T, Int) => U): IArray[U]
  /** Flat map transformation */
  def flatMap[U](f: T => IterableOnce[U]): IArray[U]
  /** Filter elements */
  def filter(p: T => Boolean): IArray[T]
  /** Filter with negation */
  def filterNot(p: T => Boolean): IArray[T]
  /** Collect partial function results */
  def collect[U](pf: PartialFunction[T, U]): IArray[U]
  
  // Slicing operations
  /** Take first n elements */
  def take(n: Int): IArray[T]
  /** Drop first n elements */
  def drop(n: Int): IArray[T]
  /** Take while predicate holds */
  def takeWhile(p: T => Boolean): IArray[T]
  /** Drop while predicate holds */
  def dropWhile(p: T => Boolean): IArray[T]
  /** Take right n elements */
  def takeRight(n: Int): IArray[T]
  /** Drop right n elements */
  def dropRight(n: Int): IArray[T]
  /** Get slice between indices */
  def slice(from: Int, until: Int): IArray[T]
  /** Split at index */
  def splitAt(n: Int): (IArray[T], IArray[T])
  
  // Combination operations
  /** Concatenate with another IArray */
  def concat[U >: T](that: IArray[U]): IArray[U]
  def ++[U >: T](that: IArray[U]): IArray[U]
  /** Prepend element */
  def prepended[U >: T](elem: U): IArray[U]
  /** Append element */
  def appended[U >: T](elem: U): IArray[U]
  /** Prepend elements */
  def prependedAll[U >: T](prefix: IterableOnce[U]): IArray[U]
  /** Append elements */
  def appendedAll[U >: T](suffix: IterableOnce[U]): IArray[U]
  
  // Searching operations
  /** Find element matching predicate */
  def find(p: T => Boolean): Option[T]
  /** Check if element exists */
  def exists(p: T => Boolean): Boolean
  /** Check if all elements match */
  def forall(p: T => Boolean): Boolean
  /** Check if contains element */
  def contains[U >: T](elem: U): Boolean
  /** Find index of element */
  def indexOf[U >: T](elem: U): Int
  def indexOf[U >: T](elem: U, from: Int): Int
  /** Find last index of element */
  def lastIndexOf[U >: T](elem: U): Int
  def lastIndexOf[U >: T](elem: U, end: Int): Int
  /** Find index where predicate holds */
  def indexWhere(p: T => Boolean): Int
  def indexWhere(p: T => Boolean, from: Int): Int
  
  // Sorting operations
  /** Sort elements */
  def sorted[U >: T](using Ordering[U]): IArray[U]
  /** Sort by key */
  def sortBy[U](f: T => U)(using Ordering[U]): IArray[T]
  /** Sort with custom comparator */
  def sortWith(lt: (T, T) => Boolean): IArray[T]
  
  // Grouping operations
  /** Remove duplicate elements */
  def distinct: IArray[T]
  /** Remove duplicates by key */
  def distinctBy[U](f: T => U): IArray[T]
  /** Group by key */
  def groupBy[K](f: T => K): Map[K, IArray[T]]
  /** Group consecutive equal elements */
  def groupMap[K, V](key: T => K)(f: T => V): Map[K, IArray[V]]
  /** Partition by predicate */
  def partition(p: T => Boolean): (IArray[T], IArray[T])
  /** Span while predicate holds */
  def span(p: T => Boolean): (IArray[T], IArray[T])
  
  // Aggregation operations
  /** Fold left */
  def foldLeft[U](z: U)(op: (U, T) => U): U
  /** Fold right */
  def foldRight[U](z: U)(op: (T, U) => U): U
  /** Reduce left */
  def reduceLeft[U >: T](op: (U, T) => U): U
  /** Reduce right */
  def reduceRight[U >: T](op: (T, U) => U): U
  /** Reduce with option */
  def reduceLeftOption[U >: T](op: (U, T) => U): Option[U]
  def reduceRightOption[U >: T](op: (T, U) => U): Option[U]
  /** Aggregate/combine */
  def aggregate[U](z: => U)(seqop: (U, T) => U, combop: (U, U) => U): U
  
  // Conversion operations
  /** Convert to Array */
  def toArray[U >: T : ClassTag]: Array[U]
  /** Convert to List */
  def toList: List[T]
  /** Convert to Vector */
  def toVector: Vector[T]
  /** Convert to Set */
  def toSet: Set[T]
  /** Convert to Map with indices */
  def toMap[K, V](using ev: T <:< (K, V)): Map[K, V]
  /** Convert to mutable buffer */
  def toBuffer: mutable.Buffer[T]
  
  // Zipping operations
  /** Zip with another collection */
  def zip[U](that: IArray[U]): IArray[(T, U)]
  /** Zip with indices */
  def zipWithIndex: IArray[(T, Int)]
  /** Zip all with filler values */
  def zipAll[U, V >: T](that: IArray[U], thisElem: V, thatElem: U): IArray[(V, U)]
  
  // Utility operations
  /** Apply side effect to each element */
  def foreach[U](f: T => U): Unit
  /** Reverse array order */
  def reverse: IArray[T]
  /** Get string representation */
  def mkString: String
  def mkString(sep: String): String
  def mkString(start: String, sep: String, end: String): String
  /** Get element count */
  def count(p: T => Boolean): Int
  /** Check if corresponds to another collection */
  def corresponds[U](that: IterableOnce[U])(p: (T, U) => Boolean): Boolean

Usage Examples:

import scala.IArray

// Creating IArrays
val numbers = IArray(1, 2, 3, 4, 5)
val empty = IArray.empty[String]
val filled = IArray.fill(3)("default")
val computed = IArray.tabulate(5)(i => i * i)
val range = IArray.range(1, 10)

// Basic operations
println(numbers.length)        // 5
println(numbers.head)          // 1
println(numbers.last)          // 5
println(numbers.isEmpty)       // false

// Transformations
val doubled = numbers.map(_ * 2)
val evens = numbers.filter(_ % 2 == 0)
val squares = numbers.map(x => x * x)

// Slicing
val first3 = numbers.take(3)           // IArray(1, 2, 3)
val without2 = numbers.drop(2)         // IArray(3, 4, 5)
val middle = numbers.slice(1, 4)       // IArray(2, 3, 4)

// Searching
val found = numbers.find(_ > 3)        // Some(4)
val index = numbers.indexOf(3)         // 2
val contains4 = numbers.contains(4)    // true

// Sorting
val names = IArray("Charlie", "Alice", "Bob")
val sorted = names.sorted              // IArray("Alice", "Bob", "Charlie")
val byLength = names.sortBy(_.length)  // IArray("Bob", "Alice", "Charlie")

// Aggregation
val sum = numbers.foldLeft(0)(_ + _)   // 15
val max = numbers.reduceLeft(_ max _)  // 5
val product = numbers.reduce(_ * _)    // 120

// Grouping
val data = IArray("apple", "banana", "apricot", "cherry")
val byFirstLetter = data.groupBy(_.head)
// Map('a' -> IArray("apple", "apricot"), 'b' -> IArray("banana"), 'c' -> IArray("cherry"))

// Combinations
val moreNumbers = IArray(6, 7, 8)
val combined = numbers ++ moreNumbers  // IArray(1, 2, 3, 4, 5, 6, 7, 8)
val withZero = numbers.prepended(0)    // IArray(0, 1, 2, 3, 4, 5)

// Zipping
val letters = IArray("a", "b", "c", "d", "e")
val pairs = numbers.zip(letters)       // IArray((1,"a"), (2,"b"), (3,"c"), (4,"d"), (5,"e"))
val indexed = numbers.zipWithIndex     // IArray((1,0), (2,1), (3,2), (4,3), (5,4))

// Conversions
val asList = numbers.toList
val asVector = numbers.toVector
val asArray = numbers.toArray
val asSet = numbers.toSet

Specialized IArray Types

IArray provides specialized implementations for primitive types for better performance.

// Specialized factory methods
object IArray:
  /** Create specialized boolean array */
  def ofBoolean(elems: Boolean*): IArray[Boolean]
  /** Create specialized byte array */
  def ofByte(elems: Byte*): IArray[Byte]
  /** Create specialized short array */
  def ofShort(elems: Short*): IArray[Short]
  /** Create specialized char array */
  def ofChar(elems: Char*): IArray[Char]
  /** Create specialized int array */
  def ofInt(elems: Int*): IArray[Int]
  /** Create specialized long array */
  def ofLong(elems: Long*): IArray[Long]
  /** Create specialized float array */
  def ofFloat(elems: Float*): IArray[Float]
  /** Create specialized double array */
  def ofDouble(elems: Double*): IArray[Double]

Usage Examples:

// Specialized arrays for better performance
val ints = IArray.ofInt(1, 2, 3, 4, 5)
val doubles = IArray.ofDouble(1.0, 2.5, 3.14)
val booleans = IArray.ofBoolean(true, false, true)

// All operations work the same
val doubled = ints.map(_ * 2)
val filtered = doubles.filter(_ > 2.0)
val count = booleans.count(identity)

Multi-dimensional Arrays

Support for creating and working with multi-dimensional immutable arrays.

object IArray:
  /** Create 2D array */
  def ofDim[T](dim1: Int, dim2: Int): IArray[IArray[T]]
  /** Create 3D array */
  def ofDim[T](dim1: Int, dim2: Int, dim3: Int): IArray[IArray[IArray[T]]]
  
  /** Fill 2D array */
  def fill[T](dim1: Int, dim2: Int)(elem: => T): IArray[IArray[T]]
  /** Fill 3D array */
  def fill[T](dim1: Int, dim2: Int, dim3: Int)(elem: => T): IArray[IArray[IArray[T]]]
  
  /** Tabulate 2D array */
  def tabulate[T](dim1: Int, dim2: Int)(f: (Int, Int) => T): IArray[IArray[T]]
  /** Tabulate 3D array */
  def tabulate[T](dim1: Int, dim2: Int, dim3: Int)(f: (Int, Int, Int) => T): IArray[IArray[IArray[T]]]

Usage Examples:

// 2D matrix
val matrix = IArray.tabulate(3, 3)((i, j) => i * 3 + j)
// IArray(IArray(0, 1, 2), IArray(3, 4, 5), IArray(6, 7, 8))

// Access elements
val element = matrix(1)(2)  // 5

// 3D tensor  
val tensor = IArray.fill(2, 2, 2)(0)

Types

// Core immutable array type
opaque type IArray[+T] = Array[? <: T]

// Companion object with factory methods
object IArray:
  def empty[T]: IArray[T]
  def apply[T](elems: T*): IArray[T]
  def fill[T](n: Int)(elem: => T): IArray[T]
  def tabulate[T](n: Int)(f: Int => T): IArray[T]
  def range(start: Int, end: Int): IArray[Int]
  def range(start: Int, end: Int, step: Int): IArray[Int]
  def from[T](it: IterableOnce[T]): IArray[T]
  def concat[T](arrays: IArray[T]*): IArray[T]
  
  // Specialized factory methods
  def ofBoolean(elems: Boolean*): IArray[Boolean]
  def ofByte(elems: Byte*): IArray[Byte]  
  def ofShort(elems: Short*): IArray[Short]
  def ofChar(elems: Char*): IArray[Char]
  def ofInt(elems: Int*): IArray[Int]
  def ofLong(elems: Long*): IArray[Long]
  def ofFloat(elems: Float*): IArray[Float]
  def ofDouble(elems: Double*): IArray[Double]
  
  // Multi-dimensional arrays
  def ofDim[T](dim1: Int, dim2: Int): IArray[IArray[T]]
  def ofDim[T](dim1: Int, dim2: Int, dim3: Int): IArray[IArray[IArray[T]]]

Install with Tessl CLI

npx tessl i tessl/maven-org-scala-lang--scala3-library-sjs1-3

docs

annotations.md

capabilities.md

collections.md

core-types.md

derivation.md

index.md

metaprogramming.md

runtime.md

utilities.md

tile.json