Raw view: Convert Code Between Languages

No invisible, zero-width, or bidi-control characters detected.

Port the {{SOURCE_LANGUAGE}} code below to {{TARGET_LANGUAGE}} ({{TARGET_VERSION}}). The result must behave identically for every input the original accepts, and it must read as if a fluent {{TARGET_LANGUAGE}} developer wrote it from scratch. Those two goals sometimes conflict; when they do, behavior wins and you document the compromise.

<code>
{{CODE}}
</code>

<target_conventions>
{{TARGET_CONVENTIONS}}
</target_conventions>

<target_conventions> may name a style guide, preferred libraries, error-handling idiom, or an existing file to match. If it is empty, follow the target language's official style guide and standard library.

Procedure:

1. Inventory the semantics that commonly break in translation and check each against <code>: integer overflow and division, string encoding and indexing, null/None/undefined handling, equality vs identity, mutable default arguments, iteration order of maps, exception hierarchy, truthiness rules, closure capture, and concurrency primitives. Note every one that <code> relies on.
2. Translate structure first, then idiom. Keep function boundaries and names recognizable so the two versions can be diffed side by side; rename only where the target's naming convention demands it (snake_case to camelCase, for example), and record every rename.
3. Replace source-language workarounds with target-language features when the behavior is provably identical (a manual loop that becomes a `map`, a try/finally that becomes a `defer` or `using`). Do not replace when the behavior differs in edge cases; keep the explicit form and add a one-line comment saying why.
4. Pick standard-library equivalents over third-party packages. When the original uses a library with no direct equivalent, state the closest option and what it does differently.
5. Walk the inventory from step 1 one more time against your output and confirm each item is preserved.

Keep comments from the original, translated where needed. Add comments only at points of semantic divergence.

## Output format

**Ported code** - one fenced code block, complete and compilable/runnable as a unit. No elisions.

**Semantic divergences** - table with columns: original construct (line), target construct, difference in behavior, how handled. Include only true divergences; if there are none, write "None: all constructs have exact equivalents."

**Renames and reshapes** - bullet list of renamed symbols and any restructuring (merged functions, split modules), each with a one-clause reason.

**Dependencies** - list of imports/packages the port needs, with version constraints if they matter.

**Verification suggestion** - 2-4 concrete inputs (including one edge case from step 1) to run through both versions to confirm equivalence.