Skillsbench python-scala-syntax-mapping
Reference guide for translating Python syntax constructs to Scala equivalents. Use when converting Python code to Scala and need mappings for basic syntax elements like variable declarations, control flow, comprehensions, string formatting, and common operators.
install
source · Clone the upstream repo
git clone https://github.com/benchflow-ai/skillsbench
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/benchflow-ai/skillsbench "$T" && mkdir -p ~/.claude/skills && cp -r "$T/tasks/python-scala-translation/environment/skills/python-scala-syntax-mapping" ~/.claude/skills/benchflow-ai-skillsbench-python-scala-syntax-mapping && rm -rf "$T"
manifest:
tasks/python-scala-translation/environment/skills/python-scala-syntax-mapping/SKILL.mdsource content
Python to Scala Syntax Mapping
Variable Declarations
| Python | Scala |
|---|---|
| (immutable) or (mutable) |
| |
| |
Prefer
val over var unless mutation is required.
Type Mappings
| Python | Scala |
|---|---|
| |
| |
| |
| |
| (of type ) or |
| or |
| |
| |
| or |
| |
Control Flow
Conditionals
# Python if x > 0: result = "positive" elif x < 0: result = "negative" else: result = "zero"
// Scala - if is an expression val result = if (x > 0) "positive" else if (x < 0) "negative" else "zero"
Loops
# Python for loop for i in range(10): print(i) for item in items: process(item) for i, item in enumerate(items): print(f"{i}: {item}")
// Scala equivalents for (i <- 0 until 10) println(i) for (item <- items) process(item) for ((item, i) <- items.zipWithIndex) println(s"$i: $item")
While loops
# Python while condition: do_something()
// Scala while (condition) { doSomething() }
Comprehensions
# Python list comprehension squares = [x ** 2 for x in range(10)] evens = [x for x in numbers if x % 2 == 0] pairs = [(x, y) for x in xs for y in ys]
// Scala for-comprehensions or map/filter val squares = (0 until 10).map(x => x * x).toList val evens = numbers.filter(_ % 2 == 0) val pairs = for { x <- xs; y <- ys } yield (x, y)
Functions
# Python def add(a: int, b: int) -> int: return a + b # Lambda square = lambda x: x ** 2 # Default arguments def greet(name: str, greeting: str = "Hello") -> str: return f"{greeting}, {name}!"
// Scala def add(a: Int, b: Int): Int = a + b // Lambda val square: Int => Int = x => x * x // or shorter: val square = (x: Int) => x * x // Default arguments def greet(name: String, greeting: String = "Hello"): String = s"$greeting, $name!"
String Formatting
| Python | Scala |
|---|---|
| |
| |
| |
Common Operators
| Python | Scala |
|---|---|
(power) | or use |
(floor div) | (for Int) or |
(modulo) | |
, , | , ` |
| |
| (reference equality) |
| (value equality) |
Exception Handling
# Python try: result = risky_operation() except ValueError as e: handle_error(e) finally: cleanup()
// Scala import scala.util.{Try, Success, Failure} // Pattern 1: try-catch try { val result = riskyOperation() } catch { case e: IllegalArgumentException => handleError(e) } finally { cleanup() } // Pattern 2: Try monad (preferred for functional style) val result = Try(riskyOperation()) match { case Success(value) => value case Failure(e) => handleError(e) }
None/Null Handling
# Python if value is None: return default return process(value) # Or result = value if value is not None else default
// Scala - use Option val result = optionValue match { case Some(v) => process(v) case None => default } // Or more concisely val result = optionValue.map(process).getOrElse(default)