Smart Inline Completions: Use Cases, Examples, and Exercises
Transpiling Code
Transpiling is the process of converting code from one high-level language to another. For example, converting Python code to JavaScript or vice versa. This can be useful when you are fluent in one language and want to write code in another language. Copilot can help you with this process by generating the equivalent code in the target language.
Let's start with simple examples. Consider the following Python code that prints the reverse of a list:
def reverse_list(lst):
return lst[::-1]
print(reverse_list([1, 2, 3, 4, 5]))
Our goal is to convert this code to JS. Start by creating a new file called app.js and copy the Python code into it as a comment.
// app.js
// def reverse_list(lst):
// return lst[::-1]
//
// print(reverse_list([1, 2, 3, 4, 5]))
To convert this code to JavaScript, you can use the following prompt:
// Transpile the Python code to JavaScript
//
// [Wait for the suggestion]
You can also use a more detailed prompt to guide Copilot in generating the JavaScript equivalent of the Python code:
// Convert the Python code that reverses a list to JavaScript
Or use a shorter one:
// JavaScript:
This is the completion that Copilot suggested for this transpilation:
// def reverse_list(lst):
// return lst[::-1]
// print(reverse_list([1, 2, 3, 4, 5]))
// In javascript:
function // <- [You can guide Copilot to generate a JS function]
// [Wait for the suggestion]
This is the full code including Copilot's suggestion:
// def reverse_list(lst):
// return lst[::-1]
// print(reverse_list([1, 2, 3, 4, 5]))
// In javascript:
function reverseList(lst) {
return lst.reverse();
}
console.log(reverseList([1, 2, 3, 4, 5]));
Try It Yourself: The following code in JavaScript checks if a number is prime. If it is the case, it returns its factorial. Otherwise, it returns the number itself.
Building with GitHub Copilot
From Autocomplete to Autonomous AgentsEnroll now to unlock all content and receive all future updates for free.
