用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/cxcscmu/SkillLearnBench --skill scala-adt-enums命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
基于 SOC 职业分类
正在显示 SKILL.md
| name | scala-adt-enums |
| description | Translating Python Enums and Protocols to Scala sealed traits and algebraic data types |
from enum import Enum
class TokenType(Enum):
STRING = "string"
NUMERIC = "numeric"
TEMPORAL = "temporal"
STRUCTURED = "structured"
BINARY = "binary"
NULL = "null"
# Usage
t = TokenType.STRING
t.value # "string"
t.name # "STRING"
sealed trait TokenType {
def value: String
}
object TokenType {
case object STRING extends TokenType { val value = "string" }
case object NUMERIC extends TokenType { val value = "numeric" }
case object TEMPORAL extends TokenType { val value = "temporal" }
case object STRUCTURED extends TokenType { val value = "structured" }
case object BINARY extends TokenType { val value = "binary" }
case object NULL extends TokenType { val value = "null" }
// Helper for pattern matching
def all: List[TokenType] = List(STRING, NUMERIC, TEMPORAL, STRUCTURED, BINARY, NULL)
}
// Usage
val t: TokenType = TokenType.STRING
t.value // "string"
from typing import Union
JsonValue = Union[str, int, float, bool, None, list["JsonValue"], dict[str, "JsonValue"]]
# Function handling multiple types
def process(value: Union[str, int]) -> str:
if isinstance(value, str):
return value
else:
return str(value)
sealed trait JsonValue
case class JString(value: String) extends JsonValue
case class JNumber(value: Double) extends JsonValue
case class JBoolean(value: Boolean) extends JsonValue
case object JNull extends JsonValue
case class JArray(value: Vector[JsonValue]) extends JsonValue
case class JObject(value: Map[String, JsonValue]) extends JsonValue
// Pattern matching (exhaustive)
def process(json: JsonValue): String = json match {
case JString(s) => s
case JNumber(n) => n.toString
case JBoolean(b) => b.toString
case JNull => "null"
case JArray(arr) => s"Array(${arr.size})"
case JObject(obj) => s"Object(${obj.size})"
}
from typing import Protocol, runtime_checkable
@runtime_checkable
class Tokenizable(Protocol):
def to_token(self) -> str: ...
def process(obj: Tokenizable) -> str:
return obj.to_token()
trait Tokenizable {
def toToken: String
}
def process(obj: Tokenizable): String = obj.toToken
// Or as type class (more flexible, less invasive)
trait Tokenizable[T] {
def toToken(value: T): String
}
object Tokenizable {
implicit val stringTokenizable: Tokenizable[String] = new Tokenizable[String] {
def toToken(value: String) = value
}
}
def process[T](obj: T)(implicit ev: Tokenizable[T]): String = ev.toToken(obj)
from abc import ABC, abstractmethod
from typing import Generic, TypeVar
T = TypeVar("T")
class BaseTokenizer(ABC, Generic[T]):
@abstractmethod
def tokenize(self, value: T) -> Token:
pass
def tokenize_batch(self, values: Iterable[T]) -> Iterator[Token]:
for v in values:
yield self.tokenize(v)
class StringTokenizer(BaseTokenizer[str]):
def tokenize(self, value: str) -> Token:
return Token(value, TokenType.STRING)
abstract class BaseTokenizer[T] {
def tokenize(value: T): Token
def tokenizeBatch(values: Iterable[T]): Iterator[Token] =
values.toIterator.map(tokenize)
}
class StringTokenizer extends BaseTokenizer[String] {
def tokenize(value: String): Token =
Token(value, TokenType.STRING)
}
from dataclasses import dataclass, field
from typing import Any
@dataclass(frozen=True)
class Token:
value: str
token_type: TokenType
metadata: dict[str, Any] = field(default_factory=dict)
def with_metadata(self, **kwargs: Any) -> "Token":
new_meta = {**self.metadata, **kwargs}
return Token(self.value, self.token_type, new_meta)
case class Token(
value: String,
tokenType: TokenType,
metadata: Map[String, Any] = Map()
) {
def withMetadata(pairs: (String, Any)*): Token = {
val newMeta = metadata ++ pairs.toMap
copy(metadata = newMeta)
}
}
@dataclass
class MutableTokenBatch:
tokens: list[Token] = field(default_factory=list)
_processed: bool = False
def add(self, token: Token) -> None:
if self._processed:
raise RuntimeError("Batch already processed")
self.tokens.append(token)
def mark_processed(self) -> None:
self._processed = True
class MutableTokenBatch {
private val tokens = scala.collection.mutable.ListBuffer[Token]()
private var processed = false
def add(token: Token): Unit = {
if (processed) throw new RuntimeException("Batch already processed")
tokens += token
}
def markProcessed(): Unit = {
processed = true
}
def getTokens: List[Token] = tokens.toList
}
Scala pattern matching is more powerful than Python's isinstance checks:
// Python equivalent:
if isinstance(value, str):
return value
elif isinstance(value, int):
return value.toString()
// Scala pattern matching:
value match {
case s: String => s
case i: Int => i.toString
case _ => "unknown"
}
// Even better with case classes:
value match {
case Token(v, TokenType.STRING, _) => v
case Token(v, TokenType.NUMERIC, _) => s"[NUM]$v"
case _ => "other"
}