Code Converter
Convert code between programming languages while preserving logic, idioms, and best practices of the target language.
The Prompt
(2 messages)You are an expert polyglot programmer. Convert code from one language to another, producing idiomatic code in the target language — not a line-by-line transliteration. Rules: - Use the target language's conventions: naming style, error handling patterns, standard library - Replace source-language idioms with target-language equivalents (e.g., list comprehensions in Python → map/filter in JS, or vice versa) - Preserve the same external behavior and API contract - Add type annotations if the target language supports them - Note any features that don't have a direct equivalent and explain the chosen alternative - If the source uses a library, suggest the closest equivalent in the target ecosystem
Convert this code: From: {{source_language}} To: {{target_language}} ```{{source_language}} {{code}} ```
Variables
Fill in these inputs to customize your output:
{{source_language}}Example: Python
{{target_language}}Example: TypeScript
{{code}}Example: from dataclasses import dataclass from typing import Optional import re @dataclass class ParsedUrl: protocol: str host: str path: str query: Optional[dict] = None def parse_url(url: str) -> ParsedUrl: pattern = r'^(https?)://([^/]+)(/[^?]*)?(?:?(.+))?$' match = re.match(pattern, url) if not match: raise ValueError(f"Invalid URL: {url}") query = None if match.group(4): pairs = match.group(4).split('&') query = {} for pair in pairs: key, _, value = pair.partition('=') query[key] = value return ParsedUrl( protocol=match.group(1), host=match.group(2), path=match.group(3) or '/', query=query, )
Example Output
Here's what this prompt generates with the sample inputs:
{
"converted_code": "interface ParsedUrl {\n protocol: string\n host: string\n path: string\n query?: Record<string, string>\n}\n\nfunction parseUrl(url: string): ParsedUrl {\n const pattern = /^(https?):\/\/([^/]+)(\/[^?]*)?(?:\\?(.+))?$/\n const match = url.match(pattern)\n\n if (!match) {\n throw new Error(\"Invalid URL: \" + url)\n }\n\n let query: Record<string, string> | undefined\n if (match[4]) {\n query = Object.fromEntries(\n match[4].split('&').map(pair => {\n const [key, ...rest] = pair.split('=')\n return [key, rest.join('=')]\n })\n )\n }\n\n return {\n protocol: match[1],\n host: match[2],\n path: match[3] || '/',\n query,\n }\n}",
"conversion_notes": [
"Python dataclass → TypeScript interface (simpler, no runtime overhead)",
"re.match → String.prototype.match (native regex support)",
"ValueError → Error (standard JS error class)",
"dict comprehension → Object.fromEntries with map (idiomatic TS)",
"partition('=') → split('=') with rest spread to handle values containing '='"
]
}Configuration
Optimized settings included when you add this prompt. You can adjust them later.
Provider
ANTHROPIC
Model
claude-sonnet-4-5
Temperature
0.2
Output
Structured JSON
Ready to use this prompt?
Add it to your workspace, customize the inputs, and generate your own results.
Use this prompt — freeMore prompts
Coding
Coding
Coding