用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/cxcscmu/SkillLearnBench --skill scala-collections-fp命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
基于 SOC 职业分类
正在显示 SKILL.md
| name | scala-collections-fp |
| description | Scala collections, iterators, and functional programming patterns for Python developers |
| Python | Scala | Characteristics |
|---|---|---|
list[T] | List[T] | Linked list, prepend fast |
list[T] | Vector[T] | Indexed access fast, append ok |
tuple[T, ...] | Seq[T] (umbrella type) | Immutable sequence |
set[T] | Set[T] | Unordered, unique elements |
dict[K, V] | Map[K, V] | Key-value pairs |
range(n) | (0 until n) or Range | Lazy range |
import scala.collection.mutable
val list = scala.collection.mutable.ListBuffer[Int]()
val set = scala.collection.mutable.Set[String]()
val map = scala.collection.mutable.Map[String, Int]()
# List comprehension
squares = [x * x for x in range(10)]
# Generator
def gen():
for x in range(10):
yield x * x
# Higher-order functions
list(map(lambda x: x * x, range(10)))
list(filter(lambda x: x > 10, range(20)))
// Collection methods (eager)
val squares = (0 until 10).map(x => x * x).toList
// Iterator (lazy)
def gen: Iterator[Int] = (0 until 10).iterator.map(x => x * x)
// For-comprehension (syntactic sugar)
val squares = for (x <- 0 until 10) yield x * x
// Higher-order functions
List(1, 2, 3).map(_ * 2) // List(2, 4, 6)
List(1, 2, 3, 4).filter(_ > 2) // List(3, 4)
Iterators are lazy and consume memory efficiently:
def tokenize_batch(self, values: Iterable[T]) -> Iterator[Token]:
for v in values:
yield self.tokenize(v)
# Usage
for token in tokenizer.tokenize_batch(large_list):
process(token) # Lazy - doesn't compute all at once
def tokenizeBatch(values: Iterable[T]): Iterator[Token] =
values.toIterator.map(tokenize)
// Usage
tokenizer.tokenizeBatch(largeList).foreach(token => process(token))
// Or with Iterator directly
def tokenizeBatch(values: Iterator[T]): Iterator[Token] =
values.map(tokenize)
FlatMap is essential in Scala - combines map + flatten.
# Map
nums = [1, 2, 3]
squared = [x * x for x in nums] # [1, 4, 9]
# FlatMap-like behavior
lists = [[1, 2], [3, 4], [5, 6]]
flattened = [x for sublist in lists for x in sublist] # [1,2,3,4,5,6]
# Function that returns list
def duplicate(x):
return [x, x]
result = [y for x in nums for y in duplicate(x)] # [1,1,2,2,3,3]
// Map
val nums = List(1, 2, 3)
val squared = nums.map(x => x * x) // List(1, 4, 9)
// FlatMap
val lists = List(List(1, 2), List(3, 4), List(5, 6))
val flattened = lists.flatMap(identity) // List(1,2,3,4,5,6)
// Function that returns List
def duplicate(x: Int): List[Int] = List(x, x)
val result = nums.flatMap(duplicate) // List(1,1,2,2,3,3)
Used for aggregations (Python: reduce, sum, etc.)
# Sum/reduce
total = sum([1, 2, 3, 4]) # 10
# Reduce
from functools import reduce
product = reduce(lambda a, b: a * b, [1, 2, 3, 4]) # 24
# Manual loop
result = 0
for x in [1, 2, 3, 4]:
result += x * 2
// Sum (built-in)
val total = List(1, 2, 3, 4).sum // 10
// Fold (left fold - left to right)
val product = List(1, 2, 3, 4).fold(1)((a, b) => a * b) // 24
val product = List(1, 2, 3, 4).foldLeft(1)(_ * _) // 24
// Or reduce (like Python's reduce, needs non-empty)
val product = List(1, 2, 3, 4).reduce(_ * _) // 24
// Complex aggregation
val result = List(1, 2, 3, 4).foldLeft(0)((acc, x) => acc + x * 2) // 20
from itertools import groupby
data = [("a", 1), ("a", 2), ("b", 3), ("b", 4)]
grouped = {k: list(g) for k, g in groupby(data, key=lambda x: x[0])}
# Sorting
sorted_data = sorted(data, key=lambda x: x[1], reverse=True)
val data = List(("a", 1), ("a", 2), ("b", 3), ("b", 4))
val grouped = data.groupBy(_._1) // Map("a" -> List(...), "b" -> List(...))
// Sorting
val sortedData = data.sortBy(_._2)(Ordering.Int.reverse)
// Or
val sortedData = data.sortWith((a, b) => a._2 > b._2)
Scala's collect is like Python's list comprehension with filtering:
val nums = List(1, 2, 3, 4, 5)
// collect with partial function
val evens = nums.collect { case x if x % 2 == 0 => x } // List(2, 4)
// Instead of: nums.filter(_ % 2 == 0)
// With transformation
val doubled = nums.collect { case x if x > 2 => x * 2 } // List(6, 8, 10)
Unlike Python, Scala collections are eager by default, but you can use Views:
// Eager - creates all intermediate collections
val result = (1 to 1000000)
.map(_ * 2)
.filter(_ > 100)
.take(5) // Still computes all 1M values!
// Lazy - View delays computation until forced
val result = (1 to 1000000)
.view
.map(_ * 2)
.filter(_ > 100)
.take(5)
.toList // Only computes what's needed!
scala.collection.mutable.* for mutable.toList, .toVector, .toSet - explicitly convert between typeslastOption or reverse.head(1 to 1000000) doesn't create a million-element listfor comprehension - more powerful than Python's list comprehension// Python
tokens = []
for word in words:
tokens.append(Token(word, ...))
// Scala - functional
val tokens = words.map(word => Token(word, ...))
// Scala - mutable accumulation (if really needed)
val tokens = scala.collection.mutable.ListBuffer[Token]()
for (word <- words) {
tokens += Token(word, ...)
}
val result = tokens.toList
// Python
result = []
for i, word in enumerate(words):
token = Token(word, ..., metadata={"position": i})
result.append(token)
// Scala
val result = words.zipWithIndex.map { case (word, i) =>
Token(word, TokenType.STRING, Map("position" -> i))
}
// Python
result = []
for word in words:
processed = process(word)
if processed is not None:
result.append(processed)
// Scala
val result = words.flatMap { word =>
process(word) // Returns Option[Token]
}.toList