export a function that can be used in another application
import { add as Add } from './utils'
Add(1, 2)
All generations.
import { add as Add } from './utils'
Add(1, 2)
def combineArrays(a, b):
c = a + b
c.sort()
return c
combineArrays([1,3,5], [2,4,6])
class DNA {
constructor(num){
this.num = num
}
}
let d = new DNA(1)
print(d.num)
function fast_loop(arr) {
var sum = 0;
for (var i=0; i<arr.length; i+=2) {
sum += arr[i];
}
return sum;
}
fast_loop([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
function object(obj) {
var result = obj.a;
console.log("result is: " + result);
return result;
}
object({a: 1});
class Animal {
constructor(age, name, cost) {
this.age = age;
this.name = name;
this.cost = cost;
}
getDetails() {
return `${this.name} is ${this.age} years old and costs ${this.cost}`;
}
}
const animalDetails = new Animal(4, 'dog', '$4,000');
console.log(animalDetails.getDetails());
def hash_sort(array):
hash = {}
result = []
for element in array:
if hash[element]
hash[element] += 1
else:
hash[element] = 1
for key in hash:
for i in range(hash[key]):
result.append(key)
return result
def read_file(path):
with open(path) as f:
file_content = f.read()
return file_content
content = read_file('some_file.txt')
print(content)
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
var person = new Person('john', 'Smith');
def bogo_sort(a):
while not is_sorted(a):
random.shuffle(a)
return a
function getDirection() {
var dirs = ['up', 'down', 'left', 'right']
var rand = Math.floor(Math.random() * Math.floor(4))
return dirs[rand]
}
var students = [];
function addStudent(student) {
students.push(student);
}
setTimeout(() => console.log("done"), 1000)
import requests
url = "https://www.google.com"
r = requests.get(url)
r.text
function createFunction(name: string, age: number) {
return function () {
return `my name is ${name} and my age is ${age}`
}
}
const getName = createFunction('bob', 23)
getName()
function createObject(name, age) {
return {
name: name,
age: age
}
}
function DNA(num) {
this.num = num
}
var student = {
firstName: "Mark",
lastName: "Foo",
age: 24,
favoriteMovies: ["Star Wars", "Lord of the Rings", "Star Trek"]
};
function print(person) {
console.log(person.firstName + " " + person.lastName + "is" + person.age + "years old.");
console.log("His favorite movies are:");
for (var i = 0; i < person.favoriteMovies.length; i++) {
console.log("-" + person.favoriteMovies[i]);
}
}
print(student);
{
"name": "Karen",
"age": 29,
"hair colour": "brown"
}
def save_to_file(url, file_name='', overwrite=False):
"""
Get content from url and save to file.
Default filename is url's name.
Default will use overwrite=True.
"""
try:
r = requests.get(url)
if r.status_code == requests.codes.ok:
file_name = file_name or url.split('/')[-1]
if not overwrite and os.path.exists(file_name):
print('File {} already exists.'.format(file_name))
else:
with open(file_name, 'wb') as fd:
for chunk in r.iter_content(chunk_size=1024):
fd.write(chunk)
else:
r.raise_for_status()
except requests.RequestException as e:
print(e)
class Direction {
UP: 1,
DOWN: 2,
LEFT: 3,
RIGHT: 4
}
function Student(first, last, age) {
this.firstName = first;
this.lastName = last;
this.age = age;
}
var student1 = new Student("Sarah", "Roberts", 20);
var student2 = new Student("Alex", "Jones", 19);
function Student(first, last) {
this.first = first
this.last = last
}
import requests
def get(url):
r = requests.get(url)
return r
get("https://m.weibo.cn/api/container/getIndex?containerid=102803&openApp=0")
def quicksort(lst):
if len(lst) <= 1:
return lst
pivot = lst[0]
left = [x for x in lst[1:] if x < pivot]
right = [x for x in lst[1:] if x >= pivot]
return quicksort(left) + [pivot] + quicksort(right)
quicksort([3, 2, 5, 1, 9])
function combineAndSort(arr1, arr2) {
return [...arr1, ...arr2].sort();
}
var fs = require('fs');
var file = fs.readFileSync('./file.json');
var json = JSON.parse(file);
console.log(json.name);
var arr = ['a', 'b', 'c', 'd', 'e',];
const fs = require('fs')
const jsonString = fs.readFile('./file.txt', 'utf8', function (err, data) {
if (err) {
console.log("Error reading file from disk:", err)
return
}
try {
const jsonObject = JSON.parse(data)
const props = ['b', 'c', 'd', 'e', 'f', 'g']
props.forEach(prop => {
const newObject = {
[prop]: jsonObject[prop]
}
fs.writeFile(`./${prop}.txt`, JSON.stringify(newObject), err => {
if (err) {
console.log('Error writing file', err)
} else {
console.log(`Successfully wrote file ${prop}.txt`)
}
})
})
} catch(err) {
console.log('Error parsing JSON string:', err)
}
})
extern crate iron;
#[macro_use] extern crate mime;
use iron::prelude::*;
use iron::status;
fn main() {
println!("Hello, world!");
}
def extract_from_file(filename):
f = open(filename, 'r')
lines = f.readlines()
f.close
logs = []
for line in lines:
log = line.split(',')
log = [log[0], log[1].rstrip('\n')]
logs.append(log)
return logs
function random(start, end) {
var result = Math.random() * (end - start) + start;
return Math.floor(result);
}
random(1, 6)
class Car {
constructor(age, name, cost) {
this.age = age;
this.name = name;
this.cost = cost;
}
}
let car1 = new Car(1, 'Toyota', 9000)
let car2 = new Car(2, 'Ford', 10000)
fetch('https://jsonplaceholder.typicode.com/posts/1')
.then(response => response.json())
.then(json => console.log(json))
function add(a, b) {
return a + b
}
add(1, 2)
const fs = require('fs');
fs.readFile('myfile.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
let input: [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t]
let output: [a,b,c], [d,e,f], [g,h,i], [j,k,l], [m,n,o], [p,q,r]
var a = [1, 3, 5, 7]
var b = [2, 4, 6, 8]
var c = a.Concat(b).OrderBy(x => x).ToArray();
function Car(color) {
this.color = color
}
var car = new Car('blue')
car.color
def split_to_7_files(filename):
# read file
f = open(filename, "r")
lines = f.readlines()
f.close()
# split the lines into 7 separate files
n = len(lines)
k = n // 7
for i in range(7):
# create output filename
fout_name = filename.split(".")[0] + "_" + str(i) + "." + filename.split(".")[1]
# write to file
fout = open(fout_name, "w")
for j in range(i*k, (i+1)*k):
fout.write(lines[j])
fout.close()
split_to_7_files("text.txt")
function getContent(url, timeout, callback) {
setTimeout(() => {
callback('content')
}, timeout)
}
function saveContent(content, callback) {
setTimeout(() => {
callback('content saved')
}, 1000)
}
function log(result) {
console.log(result)
}
getContent(url, timeout, (content) => {
saveContent(content, (result) => {
log(result)
})
})
function car(make, model, year) {
var myCar = {};
myCar.make = make;
myCar.model = model;
myCar.year = year;
return myCar;
}
var mycar = car("Honda", "Accord", 1998);
type Readonly<T> = {
readonly [P in keyof T]: T[P];
}
interface Point {
x: number;
y: number;
}
const p1: Readonly<Point> = {
x: 10,
y: 20,
};
p1.x = 5; // error!
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def myfunc(self):
print("Hello my name is " + self.name)
p1 = Person("John", 36)
p1.myfunc()
Generate
More than just a code generator. A tool that helps you with a wide range of tasks. All in one place.
Function from Description
Text Description to SQL Command
Translate Languages
Generate HTML from Description
Code to Explanation
Fix invalid Code
Get Test for Code
Class from Description
Regex from Description
Regex to Explanation
Git Command from Description
Linux Command
Function from Docstring
Add typing to code
Get Language from Code
Time complexity
CSS from Description
Meta Tags from Description
− sponsored −