agentsclimarketplace

Python scala builder pattern

Skill cxcscmu/SkillLearnBench/skills/b1-one-shot-claude-sonnet-4-6/python-scala-translation/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.From its SKILL.md

Install
npx -y skills add cxcscmu/SkillLearnBench --skill python-scala-builder-pattern

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

SKILL.md

2.4 KB, 577 tokens by cl100k_base, as published. Nobody here has run it

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

  • private constructor forces use of companion apply factory.
  • build() returns a function T => Token (first-class function), not a new class.
  • Use varargs (String, Any)* for key-value metadata pairs.
  • List is immutable; use :+ to append (creates new list).
  • Map ++ pairs merges 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 selfReturns new Builder(...)

What ships with it

Read from the repository

Just SKILL.md. No reference files, no scripts.

Keep looking

Skills are one crate of 325,949. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.