syntax | |||||||||||||||
C | C++ | CoffeeScript | Java | JavaScript | Kotlin | Lua | Objective-C | OCaml | Perl | PHP | Python | Ruby | Scheme | Swift | |
hello world |
#include <stdio.h> int main(int argc, char **argv) { printf("Hello, World!\n"); return 0; } |
#include <iostream> using namespace std; int main(int argc, char **argv) { cout << "Hello, World!" << endl; return 0; } |
console.log 'Hello, World!' |
public class Hello { public static void main(String[] args) { System.out.println("Hello, World!"); } } |
console.log('Hello, World!'); | Need to fill | print('Hello, World!') |
#import <Foundation/Foundation.h> int main(int argc, char **argv) { NSLog(@"Hello, World!"); return 0; } |
print_endline "Hello, World!";; | print "Hello, World!\n"; |
<? echo "Hello, World!\n"; ?> |
print 'Hello, World!' | Need to fill | (display "Hello, World!\n") | Need to fill |
extensions |
.c - source .h - header |
.cpp, .cc, .C, .cxx - source .h, .hpp, .hh, .H, .hxx - header |
.coffee, .litcoffee |
.java - source .class - compiled object |
.js | Need to fill | .lua |
.m - source .mm - source with C++ .h - header |
.ml | .pl | .php | .py | Need to fill | .scm | Need to fill |
block delimiters | {} | {} | indentation | {} | {} | Need to fill | Need to fill | {} | Need to fill | {} | {} | indentation | Need to fill | parentheses | Need to fill |
statement terminator | semicolon | semicolon | newline | semicolon | semicolon, but sometimes optional | Need to fill | newline or semicolon | semicolon | double semicolon | semicolon | semicolon | newline | Need to fill | parentheses matching | Need to fill |
import file/module |
#include <filename> #include "filename" |
#include <filename> #include "filename" |
Not applicable to this language |
import packagename.classname; import packagename.*; |
Not applicable to this language |
Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill |
end-of-line comment | // comment | // comment | # comment | // comment | // comment | Need to fill | Need to fill | Need to fill | Need to fill | # comment |
// comment # comment |
Need to fill | Need to fill | Need to fill | Need to fill |
block comment | /* comment */ | /* comment */ |
### comment ### |
/* comment */ | /* comment */ | Need to fill | Need to fill | Need to fill | Need to fill |
=pod comment =cut |
/* comment */ | Need to fill | Need to fill | Need to fill | Need to fill |
variable | |||||||||||||||
C | C++ | CoffeeScript | Java | JavaScript | Kotlin | Lua | Objective-C | OCaml | Perl | PHP | Python | Ruby | Scheme | Swift | |
assignment | a = 7; | a = 7; | a = 7 | a = 7; | a = 7; | Need to fill | Need to fill | a = 7; | Need to fill | $a = 7; | $a = 7; | a = 7 | Need to fill | Need to fill | Need to fill |
swap |
// basic tmp = x; x = y; y = tmp; // function void swap(int *a, int *b) { int tmp; tmp = *b; *b = *a; *a = tmp; } swap(&x, &y); // no temporary variable x = x + y; y = x - y; x = x - y; // no temporary variable 2 x = x ^ y; y = x ^ y; x = x ^ y; |
swap(x, y); | [x, y] = [y, x] |
tmp = x; x = y; y = tmp; |
// basic tmp = x; x = y; y = tmp; // tricky one-line x = [y, y = x][0]; // ES2015 (ES6) [x, y] = [y, x] |
tmp = x x = y y = tmp |
x, y = y, x |
tmp = x; x = y; y = tmp; |
Need to fill | ($x, $y) = ($y, $x); |
// basic list($x, $y) = array($y, $x); // function function swap(&$x, &$y) { $tmp = $x; $x = $y; $y = $tmp; } |
x, y = y, x | x, y = y, x | Need to fill | swap(&x, &y) |
arithmetic | |||||||||||||||
C | C++ | CoffeeScript | Java | JavaScript | Kotlin | Lua | Objective-C | OCaml | Perl | PHP | Python | Ruby | Scheme | Swift | |
power | Need to fill | Need to fill | Need to fill | Math.pow(2.5, 13.2); | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill |
square root | Need to fill | Need to fill | Need to fill | Math.sqrt(1.69); | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill |
logic | |||||||||||||||
C | C++ | CoffeeScript | Java | JavaScript | Kotlin | Lua | Objective-C | OCaml | Perl | PHP | Python | Ruby | Scheme | Swift | |
boolean type | int | bool | Need to fill | boolean |
Not applicable to this language |
Need to fill | Need to fill | Need to fill | Need to fill |
Not applicable to this language |
Not applicable to this language |
Need to fill | Need to fill | Need to fill | Need to fill |
string | |||||||||||||||
C | C++ | CoffeeScript | Java | JavaScript | Kotlin | Lua | Objective-C | OCaml | Perl | PHP | Python | Ruby | Scheme | Swift | |
concatenate |
const char *s1 = "hello"; const char *s2 = " world"; char *s3 = (char *)malloc(strlen(s1)+strlen(s2)+1); strcpy(s3, s1); strcat(s3, s2); |
// destructive string s1 = "hello"; s1 += " world"; // non-destructive string s1 = "hello"; string s2 = s1 + " world"; |
'hello' + ' world' | "hello" + " world" | 'hello' + ' world' | "hello" + " world" | 'hello' .. ' world' |
// destructive NSMutableString *s1 = [@"hello" mutableCopy]; [s1 appendString:@" world"]; // non-destructive NSString *s1 = @"hello"; NSString *s2 = [s1 stringByAppendingString:@" world"]; |
Need to fill | 'hello' . ' world' | 'hello' . ' world' | 'hello' + ' world' | 'hello' + ' world' | Need to fill | "hello" + " world" |
convert case | Need to fill | Need to fill | Need to fill |
"hello".toUpperCase(); "HELLO".toLowerCase(); |
Need to fill | Need to fill | Need to fill |
[@"hello" uppercaseString]; [@"HELLO" lowercaseString]; |
Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill |
trim | Need to fill | Need to fill | Need to fill | " hello ".trim(); | Need to fill | Need to fill | Need to fill | [@" hello " stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill |
format string | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | [NSString stringWithFormat:@"str-%@, int-%d, num-%f", @"Lang", 5, 9.4]; | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill |
join a string array |
const char *const strs[] = {"one", "two", "three"}; int i = 0; int size = 0; char *out; for (i=0;i<3;i++) { size += strlen(strs[i]) + 1; } out = (char *)malloc(size); strcpy(out, strs[0]); for (i=1;i<3;i++) { strcat(out, ","); strcat(out, strs[i]); } |
// one-liner vector<string> strs = {"one", "two", "three"}; string out = accumulate(next(strs.begin()), strs.end(), strs[0], [](const string &a, const string &b) { return a + "," + b; }); // prevent reallocation vector<string> strs = {"one", "two", "three"}; ostringstream os; copy(strs.begin(), strs.end()-1, ostream_iterator<string>(os, ",")); os << *strs.rbegin(); string out = os.str(); |
strs = ['one','two','three'] out = strs.join ',' |
// Java 8 List<String> strs = Arrays.asList("one", "two", "three"); String out = String.join(",", strs); // Java 7 and below String[] strs = {"one", "two", "three"}; StringBuilder sb = new StringBuilder(); boolean first = true; for (String str : strs) { if (first) { first = false; } else { sb.append(","); } sb.append(str); } String out = sb.toString(); |
let strs = ['one','two','three'] let out = strs.join(',') |
val strs = arrayOf("one", "two", "three") val out = strs.joinToString(",") |
strs = {"one", "two", "three"} table.concat(strs, ",") |
NSArray *strs = @[@"one", @"two", @"three"]; NSString *out = [strs componentsJoinedByString:@","]; |
Need to fill |
@strs = qw(one two three); $out = join ',', @strs; |
$strs = array('one', 'two', 'three'); $out = implode(',', $strs); |
strs = ['one','two','three'] ','.join(strs) |
strs = %w(one two three) out = strs.join ',' |
Need to fill |
let strs = ["one", "two", "three"] let out = strs.joined(separator: ",") |
split a string | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | [@"a,b,c" componentsSeparatedByString:@","]; | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill |
function | |||||||||||||||
C | C++ | CoffeeScript | Java | JavaScript | Kotlin | Lua | Objective-C | OCaml | Perl | PHP | Python | Ruby | Scheme | Swift | |
define function |
int sum(int x, int y, int z) { return x + y + z; } |
int sum(int x, int y, int z) { return x + y + z; } |
sum = (x, y, z) -> x + y + z |
int sum(int x, int y, int z) { return x + y + z; } |
function sum(x, y, z) { return x + y + z; } var sum = function (x, y, z) { return x + y + z; }; |
Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill |
def sum(x, y, z): return x + y + z |
Need to fill | Need to fill | Need to fill |
invoke function | sum(1, 2, 3); | sum(1, 2, 3); |
sum 1, 2, 3 sum(1, 2, 3) |
sum(1, 2, 3); | sum(1, 2, 3); | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | sum(1, 2, 3) | Need to fill | Need to fill | Need to fill |
default argument |
Not applicable to this language |
double logarithm(double x, double base=10) { return log(x) / log(base); } |
logarithm = (x, base=10) -> Math.log(x) / Math.log(base) |
double logarithm(double x) { return logarithm(x, 10); } double logarithm(double x, double base) { return Math.log(x) / Math.log(base); } |
function logarithm(x, base) { if (base==null) { base = 10; } return Math.log(x) / Math.log(base); } |
fun logarithm(x: Double, base: Double=10.0): Double { return Math.log(x) / Math.log(base) } |
function logarithm(x, base) base = base or 10 return math.log(x) / math.log(base) end} |
Not applicable to this language |
Need to fill |
sub logarithm { my $x = shift; my $base = shift // 10; log($x) / log($base); } |
function logarithm($x, $base=10) { return log($x) / log($base); } |
def logarithm(x, base=10): return math.log(x) / math.log(base) |
def logarithm(x, base=10) Math.log(x) / Math.log(base) end |
Need to fill |
func logarithm(x: Double, base: Double=10) -> Double { return log(x) / log(base) } |
control flow statement | |||||||||||||||
C | C++ | CoffeeScript | Java | JavaScript | Kotlin | Lua | Objective-C | OCaml | Perl | PHP | Python | Ruby | Scheme | Swift | |
if |
if (a<b) { order = -1; } else if (a==b) { order = 0; } else { order = 1; } |
if (a<b) { order = -1; } else if (a==b) { order = 0; } else { order = 1; } |
Need to fill |
if (a<b) { order = -1; } else if (a==b) { order = 0; } else { order = 1; } |
if (a<b) { order = -1; } else if (a==b) { order = 0; } else { order = 1; } |
Need to fill | Need to fill | Need to fill | Need to fill |
if ($a<$b) { $order = -1; } elsif ($a==$b) { $order = 0; } else { $order = 1; } |
if ($a<$b) { $order = -1; } elseif ($a==$b) { $order = 0; } else { $order = 1; } |
Need to fill | Need to fill | Need to fill | Need to fill |
resizable array | |||||||||||||||
C | C++ | CoffeeScript | Java | JavaScript | Kotlin | Lua | Objective-C | OCaml | Perl | PHP | Python | Ruby | Scheme | Swift | |
create |
Not applicable to this language |
#include <vector> std::vector<int> a; |
a = new Array() a = [] |
import java.util.ArrayList; ArrayList a = new ArrayList(); // legacy class(slow due to synchronization) import java.util.Vector; Vector a = new Vector(); |
a = new Array(); a = []; |
Need to fill | Need to fill | Need to fill | Need to fill | @a = (); |
$a = array(); $a = []; |
Need to fill | Need to fill | Need to fill | Need to fill |
create with values | Need to fill | Need to fill |
a = new Array(1, 2, 3) a = [1, 2, 3] |
Not applicable to this language |
a = new Array(1, 2, 3); a = [1, 2, 3]; |
Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill |
regular expression | |||||||||||||||
C | C++ | CoffeeScript | Java | JavaScript | Kotlin | Lua | Objective-C | OCaml | Perl | PHP | Python | Ruby | Scheme | Swift | |
replace first | Need to fill | Need to fill | Need to fill |
String consonants = "hello".replaceFirst("[aeiou]", ""); import java.util.regex.Pattern; String consonants = Pattern.compile("[aeiou]").matcher("hello").replaceFirst(""); |
Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill |
replace all | Need to fill | Need to fill | Need to fill |
String consonants = "hello".replaceAll("[aeiou]", ""); import java.util.regex.Pattern; String consonants = Pattern.compile("[aeiou]").matcher("hello").replaceAll(""); |
Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill |
capturing group |
const char *str = "2016-12-05"; regex_t rx; if (regcomp(&rx, "([[:digit:]]{4})-([[:digit:]]{2})-([[:digit:]]{2})", REG_EXTENDED)==0) { regmatch_t m[4]; if (regexec(&rx, str, 4, m, 0)==0) { int year = str2int(str+m[1].rm_so, m[1].rm_eo - m[1].rm_so); int month = str2int(str+m[2].rm_so, m[2].rm_eo - m[2].rm_so); int day = str2int(str+m[3].rm_so, m[3].rm_eo - m[3].rm_so); } regfree(&rx); } |
string str("2016-12-05"); regex rx("(\\d{4})-(\\d{2})-(\\d{2})"); smatch m; if (regex_match(str, m, rx)) { int year = stoi(m.str(1)); int month = stoi(m.str(2)); int day = stoi(m.str(3)); } |
str = '2016-12-05' if /(\d{4})-(\d{2})-(\d{2})/.test str year = Number RegExp.$1 month = Number RegExp.$2 day = Number RegExp.$3 |
String str = "2016-12-05"; Pattern rx = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})"); Matcher m = rx.matcher(str); if (m.matches()) { int year = Integer.parseInt(m.group(1)); int month = Integer.parseInt(m.group(2)); int day = Integer.parseInt(m.group(3)); } |
var str = '2016-12-05'; if (/(\d{4})-(\d{2})-(\d{2})/.test(str)) { var year = Number(RegExp.$1); var month = Number(RegExp.$2); var day = Number(RegExp.$3); } |
val str = "2016-12-05" val rx = "(\\d{4})-(\\d{2})-(\\d{2})".toRegex() val m = rx.matchEntire(str) if (m!=null) { val year = m.groups[1]!!.value.toInt() val month = m.groups[2]!!.value.toInt() val day = m.groups[3]!!.value.toInt() } |
str = '2016-12-05' rx = '(%d%d%d%d)-(%d%d)-(%d%d)' year, month, day = string.match(str, rx) year = tonumber(year) month = tonumber(month) day = tonumber(day) |
NSString *str = @"2016-12-05"; NSRegularExpression *rx = [NSRegularExpression regularExpressionWithPattern:@"(\\d{4})-(\\d{2})-(\\d{2})" options:0 error:nil]; NSTextCheckingResult *m = [rx firstMatchInString:str options:0 range:NSMakeRange(0, [str length])]; if (m!=nil) { NSInteger year = [[str substringWithRange:[m rangeAtIndex:1]] integerValue]; NSInteger month = [[str substringWithRange:[m rangeAtIndex:2]] integerValue]; NSInteger day = [[str substringWithRange:[m rangeAtIndex:3]] integerValue]; } |
Need to fill |
$str = '2016-12-05'; $rx = qr/(\d{4})-(\d{2})-(\d{2})/; if ($str =~ $rx) { ($year, $month, $day) = (int $1, int $2, int $3); } |
$str = '2016-12-05'; $rx = '/(\d{4})-(\d{2})-(\d{2})/'; if (preg_match($rx, $str, $m)) { $year = (int)$m[1]; $month = (int)$m[2]; $day = (int)$m[3]; // if you don't need integers list($_, $year, $month, $day) = $m; } |
str = '2016-12-05' rx = '(\\d{4})-(\\d{2})-(\\d{2})' m = re.search(rx, str) if m: year = int(m.group(1)) month = int(m.group(2)) day = int(m.group(3)) // if you don't need integers year, month, day = m.groups() |
// using method str = '2016-12-05' rx = /(\d{4})-(\d{2})-(\d{2})/ m = rx.match(str) if m year = m[1].to_i month = m[2].to_i day = m[3].to_i // if you don't need integers year, month, day = m[1..3] end // using pattern-matching operator str = '2016-12-05' rx = /(\d{4})-(\d{2})-(\d{2})/ if rx =~ str year = $~[1].to_i month = $~[2].to_i day = $~[3].to_i // if you don't need integers year, month, day = $~[1..3] end |
Need to fill |
let str = "2016-12-05" let rx = try! NSRegularExpression(pattern: "(\\d{4})-(\\d{2})-(\\d{2})", options: []) if let m = rx.firstMatch(in: str, options: [], range: NSRange(location: 0, length: (str as NSString).length)) { let year = Int((str as NSString).substring(with: m.rangeAt(1))) let month = Int((str as NSString).substring(with: m.rangeAt(2))) let day = Int((str as NSString).substring(with: m.rangeAt(3))) } |
date | |||||||||||||||
C | C++ | CoffeeScript | Java | JavaScript | Kotlin | Lua | Objective-C | OCaml | Perl | PHP | Python | Ruby | Scheme | Swift | |
current Unix time | time_t now = time(NULL); | time_t now = time(nullptr); |
now = Date.now() / 1000 now = new Date().getTime() / 1000 |
long now = System.currentTimeMillis() / 1000; long now = new Date().getTime() / 1000; |
now = Date.now() / 1000; now = new Date().getTime() / 1000; |
val now = System.currentTimeMillis() / 1000 val now = Date().getTime()/1000 |
now = os.time() | NSTimeInterval now = [[NSDate date] timeIntervalSince1970]; | Need to fill | $now = time; | $now = time(); | time.time() | now = Time.now.to_i | Need to fill | let now = Date().timeIntervalSince1970 |
object-oriented programming | |||||||||||||||
C | C++ | CoffeeScript | Java | JavaScript | Kotlin | Lua | Objective-C | OCaml | Perl | PHP | Python | Ruby | Scheme | Swift | |
define class | Need to fill | Need to fill | Need to fill |
public class Shape2D { private double x; private double y; public Shape2D(double x, double y) { this.x = x; this.y = y; } public void move(double dx, double dy) { x += dx; y += dy; } } |
Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill |
create object | Need to fill | Need to fill | Need to fill | Shape2D s = new Shape2D(5, 3); | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill |
invoke method | Need to fill | Need to fill | Need to fill | s.move(2, 4); | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill |
subclass | Need to fill | Need to fill | Need to fill |
public class Circle extends Shape2D { private double radius; public Circle(double x, double y, double radius) { super(x, y); this.radius = radius; } } |
Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill |
root class | Need to fill | Need to fill | Need to fill | java.lang.Object | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill | Need to fill |