Command Palette

Search for a command to run...

UnylyUnyly
Назад к скиллам

Scala Collections

БесплатноБез исполняемых скриптовНе проверен

Use when scala collections including immutable/mutable variants, List, Vector, Set, Map operations, collection transformations, lazy evaluation with views…

TheBushidoCollectiveот TheBushidoCollective★ 202 звезды на GitHub

Об этом скилле

Scala Collections

Introduction

Scala's collections library is one of its most powerful features, providing a rich, unified API for working with sequences, sets, and maps. The library emphasizes immutability by default while offering mutable alternatives when needed for performance-critical code.

The collections hierarchy distinguishes between immutable and mutable variants, with immutable collections being the default. Key collection types include List, Vector, Set, Map, Array, and their specialized variants. The library provides consistent transformation operations across all collection types.

This skill covers immutable vs mutable collections, sequences (List, Vector, Array), sets and maps, collection operations (map, filter, fold), for-comprehensions, lazy evaluation, parallel collections, and performance characteristics.

Immutable vs Mutable Collections

Immutable collections provide thread safety and predictability, while mutable collections offer performance benefits for intensive updates.

// Immutable List (default)
val immutableList = List(1, 2, 3, 4, 5)
val newList = immutableList :+ 6  // Creates new list
val prepended = 0 :: immutableList  // Prepends element

// Original unchanged
println(immutableList)  // List(1, 2, 3, 4, 5)
println(newList)        // List(1, 2, 3, 4, 5, 6)

// Mutable ListBuffer
import scala.collection.mutable

val mutableList = mutable.ListBuffer(1, 2, 3)
mutableList += 4        // Mutates in place
mutableList ++= List(5, 6)
mutableList -= 2

println(mutableList)    // ListBuffer(1, 3, 4, 5, 6)

// Immutable Set
val immutableSet = Set(1, 2, 3)
val addedSet = immutableSet + 4
val removedSet = immutableSet - 2

// Mutable Set
val mutableSet = mutable.Set(1, 2, 3)
mutableSet += 4
mutableSet -= 2

// Immutable Map
val immutableMap = Map("a" -> 1, "b" -> 2, "c" -> 3)
val updatedMap = immutableMap + ("d" -> 4)
val removedMap = immutableMap - "b"

// Mutable Map
val mutableMap = mutable.Map("a" -> 1, "b" -> 2)
mutableMap("c") = 3
mutableMap += ("d" -> 4)
mutableMap -= "b"

// Converting between immutable and mutable
val immutable = List(1, 2, 3)
val asMutable = immutable.toBuffer  // Mutable copy
asMutable += 4

val backToImmutable = asMutable.toList

// Immutable Vector (efficient random access)
val vector = Vector(1, 2, 3, 4, 5)
val updatedVector = vector.updated(2, 10)  // Efficiently creates new vector

// Choosing between immutable and mutable
// Use immutable for:
// - Default choice
// - Concurrent access
// - Functional transformations
// - Public APIs

// Use mutable for:
// - Performance-critical loops
// - Large-scale updates
// - Local scope only
// - Builder patterns

// Builder pattern with immutable result
def buildList(): List[Int] = {
  val builder = List.newBuilder[Int]
  for (i <- 1 to 100) {
    builder += i
  }
  builder.result()
}

// Immutable collection with updates
case class User(name: String, age: Int, email: String)

val users = List(
  User("Alice", 30, "[email protected]"),
  User("Bob", 25, "[email protected]")
)

val updatedUsers = users.map { user =>
  if (user.name == "Alice") user.copy(age = 31)
  else user
}

Prefer immutable collections by default for safety and simplicity, using mutable collections only when profiling shows performance bottlenecks.

Sequences: List, Vector, and Array

Different sequence types offer varying performance characteristics for different access patterns.

// List: Linked list, O(1) prepend, O(n) random access
val list = List(1, 2, 3, 4, 5)

val prepended = 0 :: list           // O(1)
val concatenated = list ::: List(6, 7)  // O(n)
val appended = list :+ 6            // O(n)

// Pattern matching on lists
def sumList(list: List[Int]): Int = list match {
  case Nil => 0
  case head :: tail => head + sumList(tail)
}

// List construction
val range = List.range(1, 11)       // List(1, 2, ..., 10)
val filled = List.fill(5)(0)        // List(0, 0, 0, 0, 0)
val tabulated = List.tabulate(5)(i => i * i)  // List(0, 1, 4, 9, 16)

// Vector: Indexed sequence, O(log32 n) for all operations
val vector = Vector(1, 2, 3, 4, 5)

val vectorUpdated = vector.updated(2, 10)  // O(log n)
val vectorAppended = vector :+ 6            // O(log n)
val vectorPrepended = 0 +: vector           // O(log n)

// Random access
println(vector(3))  // O(log n) - efficient

// Vector is better for:
// - Random access
// - Both-end operations
// - Large collections

// Array: Mutable, fixed-size, O(1) random access
val array = Array(1, 2, 3, 4, 5)

array(2) = 10  // Mutable update
println(array.mkString(", "))

// Array operations return Arrays
val doubled = array.map(_ * 2)

// ArrayBuffer: Mutable, resizable
import scala.collection.mutable.ArrayBuffer

val buffer = ArrayBuffer(1, 2, 3)
buffer += 4
buffer ++= Array(5, 6)
buffer.insert(0, 0)
buffer.remove(2)

// Seq: General sequence trait
def processSeq(seq: Seq[Int]): Int = seq.sum

println(processSeq(List(1, 2, 3)))
println(processSeq(Vector(1, 2, 3)))
println(proce
Полный SKILL.md на GitHub

Установить Scala Collections в Claude Code и Claude Desktop

Зарегайся, чтобы установить скилл

Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.

  • Открой команду установки в одну строку
  • Сохраняй скиллы в синхронизируемую библиотеку
  • Уведомления, когда скиллы обновляются
Зарегаться бесплатноУ меня уже есть аккаунт

Разрешённые инструменты

Инструменты, которые скиллу разрешено вызывать.

Без ограничений — скилл может использовать любой инструмент.

FAQ

Что делает скилл Scala Collections?

Use when scala collections including immutable/mutable variants, List, Vector, Set, Map operations, collection transformations, lazy evaluation with views, parallel collections, and custom collection builders for efficient data processing.

Как установить скилл Scala Collections?

Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.

Скилл Scala Collections запускает скрипты?

Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.

Похожие скиллы

Сравнить Scala Collections с