python-scala-builder-pattern
Implementing immutable fluent builder pattern in Scala as a translation of Python classes with method chaining, emphasizing immutability and type safety.
- License 许可
- MIT
- 最近更新
- 2026-10-02
- Stars
- ★ 92
- Forks
- 7
01 前言 Front Matter
- name
- python-scala-builder-pattern
- description
- Implementing immutable fluent builder pattern in Scala as a translation of Python classes with method chaining, emphasizing immutability and type safety.
03 SKILL.md 正文
Builder Pattern in Scala
Python Pattern (Mutable Builder)
class TokenizerBuilder(Generic[T]):
def __init__(self):
self._normalizers = []
self._metadata = {}
def with_normalizer(self, normalizer):
self._normalizers.append(normalizer)
return self
def build(self):
...
Scala Idiomatic Translation (Immutable Builder)
In Scala, prefer immutable builders — each method creates a new builder instance:
class TokenizerBuilder[T] private (
normalizers: List[String => String],
validators: List[T => Boolean],
meta: Map[String, Any]
) {
def withNormalizer(f: String => String): TokenizerBuilder[T] =
new TokenizerBuilder(normalizers :+ f, validators, meta)
def withValidator(f: T => Boolean): TokenizerBuilder[T] =
new TokenizerBuilder(normalizers, validators :+ f, meta)
def withMetadata(pairs: (String, Any)*): TokenizerBuilder[T] =
new TokenizerBuilder(normalizers, validators, meta ++ pairs)
def build(): T => Token = { value =>
validators.foreach { v =>
if (!v(value)) throw new IllegalArgumentException(s"Validation failed for $value")
}
val str = normalizers.foldLeft(value.toString)((s, f) => f(s))
Token(str, TokenType.STRING, meta)
}
}
object TokenizerBuilder {
def apply[T](): TokenizerBuilder[T] =
new TokenizerBuilder[T](Nil, Nil, Map.empty)
}
Usage
val tokenizer = TokenizerBuilder[String]()
.withNormalizer(_.toLowerCase)
.withNormalizer(_.replace(" ", "_"))
.withValidator(_.nonEmpty)
.withMetadata("type" -> "custom")
.build()
val token = tokenizer("Hello World")
// token.value == "hello_world"
Key Scala Conventions
privateconstructor forces use of companionapplyfactory.build()returns a functionT => Token(first-class function), not a new class.- Use varargs
(String, Any)*for key-value metadata pairs. Listis immutable; use:+to append (creates new list).Map ++ pairsmerges maps without mutation.
Mutable vs Immutable
| Python (mutable) | Scala (immutable) |
|---|---|
self._list.append(x) |
new Builder(list :+ x, ...) |
self._dict.update(d) |
new Builder(..., map ++ d) |
Returns self |
Returns new Builder(...) |
04 同仓库更多 Skills
object_counter
Count occurrences of an object in the image using computer vision algorithm.
travel-planning-workflow
Invoke this skill when performing travel planning. It defines a structured workflow for generating itineraries. Following this workflow is essential to ensure the final plan satisfies user requirements.
bundled-dataset-search-tool
invoke this skill when you need to perform database search for travel planning. This skill provides some useful pre-packaged tools to look up accommodations, attractions, cities, driving distance, flights, and restaurants from the bundled dataset.
glm-output
Output-processing guidance for GLM tasks. Especially useful after glm-basics and glm-calibration when you need verifier-matching metrics from output.nc and a final /root/metrics.json.
glm-calibration
Calibration guidance for GLM tasks. Often most effective after glm-basics has clarified the setup; glm-output is the companion skill for exact final metric computation.
glm-basics
A good starting point for GLM calibration tasks. Use to inspect glm3.nml, confirm how GLM runs, and identify the relevant files before moving on to calibration and output evaluation.