agentsclimarketplace

Run2 scala immutable builder

Skill cxcscmu/SkillLearnBench/skills/b2-self-feedback-claude-haiku-4-5/python-scala-translation/run2_scala-immutable-builder

Building immutable configurations with fluent method chaining in ScalaFrom its SKILL.md

Install
npx -y skills add cxcscmu/SkillLearnBench --skill run2_scala-immutable-builder

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

SKILL.md

2.2 KB, 516 tokens by cl100k_base, as published. Nobody here has run it

Fluent Builder Pattern in Scala

Method Chaining with this.type

The this.type singleton type enables proper method chaining while maintaining type safety.

class TokenizerBuilder[T] {
  private var normalizers: List[String => String] = List()
  private var metadata: Map[String, Any] = Map()

  def withNormalizer(normalizer: String => String): this.type = {
    normalizers = normalizers :+ normalizer
    this
  }

  def withMetadata(entries: (String, Any)*): this.type = {
    metadata = metadata ++ entries.toMap
    this
  }
}

// Usage - returns actual type, not generic Builder
val builder: TokenizerBuilder[String] = TokenizerBuilder[String]()
  .withNormalizer(_.toLowerCase)
  .withMetadata("key" -> "value")

Immutable Builder Alternative

For true immutability, create new instances (heavier but safer):

case class Config[T](
  normalizers: List[String => String] = List(),
  metadata: Map[String, Any] = Map()
) {
  def withNormalizer(f: String => String) = copy(
    normalizers = normalizers :+ f
  )
}

Converting to Immutable Result

def build(): T => Token = { value =>
  // Capture current state (immutable snapshot)
  val capturedNormalizers = normalizers
  val capturedMetadata = metadata

  { v =>
    var str = v.toString
    for (norm <- capturedNormalizers) {
      str = norm(str)
    }
    Token(str, STRING, capturedMetadata)
  }
}

Validation During Build

def build(): T => Token = { value =>
  val errors = mutable.ArrayBuffer[String]()

  for (validator <- validators) {
    if (!validator(value)) {
      errors += s"Validation failed for $value"
    }
  }

  if (errors.nonEmpty) {
    throw new IllegalArgumentException(errors.mkString("; "))
  }

  // Build tokenizer function
  val capturedMetadata = metadata
  v => Token(v.toString, STRING, capturedMetadata)
}

Factory Object Companion

class TokenizerBuilder[T] { ... }

object TokenizerBuilder {
  def apply[T](): TokenizerBuilder[T] = new TokenizerBuilder[T]
}

// Usage
TokenizerBuilder[String]()
  .withNormalizer(_.trim)
  .build()

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.