Run2 scala immutable builder
Implementing the builder pattern in Scala using immutable case classes or private constructors with copy.From its SKILL.md
npx -y skills add cxcscmu/SkillLearnBench --skill run2_scala-immutable-builderAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
SKILL.md
1.1 KB, 216 tokens by cl100k_base, as published. Nobody here has run it
Immutable Builder Pattern in Scala
Using Private Constructor + Companion Object
class Builder[T] private (
normalizers: Vector[String => String],
validators: Vector[T => Boolean],
metadata: Map[String, Any]
) {
def withNormalizer(f: String => String): Builder[T] =
new Builder(normalizers :+ f, validators, metadata)
def withMetadata(entries: (String, Any)*): Builder[T] =
new Builder(normalizers, validators, metadata ++ entries.toMap)
def build(): T => Result = { value =>
validators.foreach(v => if (!v(value)) throw new IllegalArgumentException(...))
val str = normalizers.foldLeft(value.toString)((s, f) => f(s))
Result(str, metadata)
}
}
object Builder {
def apply[T](): Builder[T] = new Builder(Vector.empty, Vector.empty, Map.empty)
}
Key: immutable collections (Vector, Map) ensure thread safety. Each with* method returns a new instance.
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.