🧑‍🏫 All The Things
C++ Clojure Elixir Go Java
JavaScript Kotlin Objective-C PHP Python
Ruby Swift TypeScript

C++ Clojure Elixir Go Java JavaScript Kotlin Objective-C PHP Python Ruby Swift TypeScript
Print std::cout << "Hello!"; (println "Hello!") IO.puts "Hello!" fmt.Println("Hello!") System.out.println("Hello!"); console.log('Hello!'); println("Hello!") NSLog(@"Hello!"); echo('Hello!'); print("Hello!") puts "Hello!" print("Hello!") console.log('Hello!');
Comment // Awesome! ; Awesome! # Awesome! // Awesome! // Awesome! // Awesome! // Awesome! // Awesome! // Awesome! # Awesome! # Awesome! // Awesome! // Awesome!
Inline Comment /* Cool! */ -- -- /* Cool! */ /* Cool! */ /* Cool! */ /* Cool! */ /* Cool! */ /* Cool! */ -- -- /* Cool! */ /* Cool! */
Declare a constant const int foo = 42; (def foo 42) @foo 42 foo := 42 int foo = 42; const foo = 42; val foo = 42 const int foo = 42; const foo = 42; FOO = 42 Foo = 42 let foo = 42 const foo:number = 42;
Declare a variable int bar = 100; (def bar 100) bar = 100 bar := 100 int bar = 100; let bar = 100; var foo = 42 int bar = 100; $bar = 100; bar = 100 bar = 100 var bar = 100 let bar:number = 100;
Increment i++ (inc i) i += 1 i++ i++ i++ i++ i++ $i++ i++ i += 1 i += 1 i++
Conditional if (maybe) { ... } else { ... } (if maybe ... ...) if maybe, do: ..., else: ... if maybe { ... } else { ... } if (maybe) { ... } else { ... } if (maybe) { ... } else { ... } if (maybe) { ... } else { ... } if (maybe) { ... } else { ... } if ($maybe) { ... } else { ... } if maybe: ... else: ... if maybe ... else ... end if maybe { ... } else { ... } if (maybe) { ... } else { ... }
For Loop for (int i = 0; i < 10; i++) { } (repeat 10 ...) for i <- 0..10, do: ... for i := 1; i < 10; i++ { } for (int i = 0; i < 10; i++) { } for (let i = 0; i < 10; i++) { } for (i in 0..10) { } for (int i = 0; i < 10; i++) { } for ($i = 0; $i < 10; $i++) { } for i in range(10): ... 10.times do 😍 end for var i in 0..<10 { } for (let i:number = 0; i < 10; i++) { }
Concatenate Strings first + " " + last (str first " " last) first <> " " <> last first + " " + last first + " " + last first + " " + last first + " " + last [first stringByAppendingString: last]; $first . " " . $last first + " " + last first + " " + last first + " " + last first + " " + last
String Interpolation -- -- "#{first} #{last}" fmt.Sprintf("%s %s", first, last) String.format("%s %s", first, last) `${first} ${last}` "$first $last" [NSString stringWithFormat:@"%@ %@", first, last] "$first $last" f'{first} {last}' "#{first} #{last}" "\(first) \(last)" `${first} ${last}`
Ternary Operator maybe ? yes : no (if maybe yes no) if maybe, do: yes, else: no -- maybe ? yes : no maybe ? yes : no if (maybe) yes else no maybe ? yes : no maybe ? yes : no yes if maybe else no maybe ? yes : no maybe ? yes : no maybe ? yes : no
Null Coalescing -- (or value default) value || default -- value == null ? value : default value || default value ?: default value == nil ? value : default $value ?? $default value or default value.nil? ? default : value value ?? default value || default
Unwrap Optional -- -- -- -- Optional<String> possibly = Optional.of("foo"); possibly.ifPresent(definitely -> ...); -- var possibly: String? = whoKnows() var definitely = possibly?.let { } -- -- -- -- let possibly: String? = whoKnows() if let definitely = possibly { } --
Declare function string hello(string name) { return "Hello, " + name + "!"; } (def hello [name] (str "Hello, " name "!")) def hello(name) do "Hello, #{name}!" end func Hello(name string) string { return fmt.Sprintf("Hello, %s!", name) } String hello(String name) { return "Hello, " + name + "!"; } function hello(name) { return `Hello, ${name}!`; } fun hello(name: String): String { return "Hello, $name!" } - (NSString *) hello:(NSString *)name { return [NSString stringWithFormat: @"Hello, %@!", name]; } function hello($name) { return "Hello, $name!"; } def hello(name): return "Hello, " + name + "!" def hello(name) return "Hello, #{name}!" end func hello(name: String) -> String { return "Hello, \(name)!" } function hello(name: string): string { return `Hello, ${name}!`; }
Call function hello("Andrew") (hello "Andrew") Greeter.hello("Andrew") Hello("Andrew") hello("Andrew") hello("Andrew") hello("Andrew") [greeter hello:@"Andrew"] hello("Andrew") hello("Andrew") hello("Andrew") hello(name: "Andrew") hello("Andrew")
Declare Object class Person { string first; string last; Person(string f, string l) { first = f; last = l; } } -- -- type Person struct { first string last string } class Person { String first; String last; Person(String first, String last) { this.first = first; this.last = last; } } class Person { constructor(first, last) { this.first = first; this.last = last; } } class Person(first: Name, last: Name) { var first = first var last = last } @interface Person: NSObject @property NSString *first; @property NSString *last; + (Line *) personWithFirst: (NSString *) first last: (NSArray *) last; @end @implementation Person + (Line *) personWithFirst: (NSString *) first last: (NSArray *) last { Person *person = [[Person alloc] init]; person.first = first; person.last = last; return person; } @end class Person { private $first; private $last; function __construct($first, $last) { $this->first = $first; $this->last = $last; } } class Person: def __init__(self, first, last): self.first = first self.last = last class Person attr_accessor :first attr_accessor :last def initialize(first, last) @first = first @last = last end end class Person { var first: String var last: String init(first: String, last: String) { self.first = first self.last = last } } class Person { first: string; last: string; constructor(first: string, last: string) { this.first = first; this.last = last; } }
New Object Person(first, last) -- -- Person{first: first, last: last} new Person(first, last) new Person(first, last) Person(first, last) [Person personWithFirst:first last:last] new Person($first, $last) Person(first, last) Person.new(first, last) Person(first:first, last:last) new Person(first, last)
Array Literal vector<string> colors = { "red", "orange", "yellow" }; (def colors ["red" "orange" "yellow"]) colors = ["red", "orange", "yellow"] colors := []string{"red", "orange", "yellow"} ArrayList<String> colors = new ArrayList<>( Arrays.asList("red", "orange", "yellow")); let colors = ["red", "orange", "yellow"]; listOf("red", "orange", "yellow") NSMutableArray *colors = [@[@"red", @"orange", @"yellow"] mutableCopy]; $colors = ["red", "orange", "yellow"]; colors = ["red", "orange", "yellow"] colors = ["red", "orange", "yellow"] var colors = ["red", "orange", "yellow"] let colors[:string] = ["red", "orange", "yellow"];
Add to Array colors.push_back("green"); (conj colors "green") ["green" | colors] colors = append(colors, "green") colors.add("green") colors.push("green") colors.add("green") [colors addObject: @"green"] array_push($colors, "green") colors.append("green") colors.push("green") colors.append("green") colors.push("green")
Search Array std::count(colors.begin(), colors.end(), "turquoise") == 0 (contains? colors "turquoise") Enum.member?(colors, "turquoise") func contains(s []string, e string) bool { for _, a := range s { if a == e { return true } } return false } colors.contains("turquoise") colors.includes("turquoise") "turquoise" in colors [colors containsObject: @"turquoise"] in_array("turquoise", $colors) "turquoise" in colors colors.include?("turquoise") colors.contains("turquoise") colors.includes("turquoise")
Remove from Array colors.pop_back() (pop colors) Enum.drop(list, -1) colors = colors[:len(colors) - 1] colors.remove(colors.size() - 1) colors.pop() colors.dropLast(1) [colors removeLastObject] array_pop($colors) colors.pop() colors.pop colors.popLast() colors.pop()
Value at Index colors.at(3) (colors 3) Enum.at(colors, 3) colors[3] colors.get(3) colors[3] colors.get(3) [colors objectAtIndex: 3] $colors[3] colors[3] colors[3] colors[3] colors[3]
First colors.front() (first colors) hd(colors) colors[0] colors.get(0) colors[0] colors.first() [colors firstObject] $colors[0] colors[0] colors.first colors.first colors[0]
Last colors.back() (last colors) Enum.take(colors, -1)[0] colors[len(colors) - 1] colors.get(colors.size() - 1) colors[colors.length - 1] colors.last() [colors lastObject] end($colors) colors[-1] colors.last colors.last colors[colors.length - 1]
Slice std::vector(colors.begin(), colors.begin() + 3) (take colors 3) Enum.take(colors, 3) colors[0:3] colors.subList(0, 3) colors.slice(0, 3) colors.sliceArray(0..3) [colors subarrayWithRange: NSMakeRange(0, 3)] array_slice($colors, 0, 3) colors[0:3] colors[0..3] colors[0...3] colors.slice(0, 3)
Enumerate Array for (auto & color : colors) { } (for [color colors] ...) Enum.each(colors, fn(color) -> ... end) for color := range colors { } for (String color : colors) { } for (let color of colors) { } for (color in colors) { } for (NSString *color in colors) { } foreach ($colors as $color) { } for color in colors: ... colors.each do |color| end for color in colors { } for (let color of colors) { }
Concat Arrays std::vector<string> more = { "blue", "purple" }; colors.insert(colors.end(), more.begin(), more.end()); (concat colors ["blue" "purple"]) colors ++ ["blue" "purple"] append(colors, []string{"blue", "purple"}...) ArrayList<String> more = new ArrayList<>( Arrays.asList("blue", "purple")); colors.addAll(more); colors.concat(["blue", "purple"]) colors + ["blue", "purple"] [colors arrayByAddingObjectsFromArray: @[@"blue", @"purple"]] array_merge($colors, ["blue", "purple"]) colors + ["blue", "purple"] colors + ["blue", "purple"] colors + ["blue", "purple"] colors.concat(["blue", "purple"])
Dictionary Literal std::map<string, string> greetings = { { "en", "hello" }, { "fr", "bonjour" } }; (def greetings (hash-map "en" "hello" "fr" "bonjour")) %{:en => "hello", :fr => "bonjour"} var lines = map[string]string{ "en": "hello", "fr": "bonjour"} Map<String, String> greetings = new HashMap<>() {{ put("en", "hello"); put("fr", "bonjour"); }}; let greetings = {en: "hello", fr: "bonjour"}; val lines: HashMap<String,String> = hashMapOf("en" to "hello", "fr" to "bonjour") NSMutableDictionary *greetings = [@{@"en" : @"hello", @"fr" : @"bonjour"} mutableCopy]; $greetings = ["en" => "hello", "fr" => "bonjour"]; greetings = {"en": "hello", "fr": "bonjour"} greetings = {"en" => "hello", "fr" => "bonjour"} var greetings = ["en": "hello", "fr": "bonjour"] interface Dictionary { en: string, fr: string, ru: string; jp: string } let greetings: Dictionary = {en: "hello", fr: "bonjour"};
Get Value greetings.at("fr") (get greetings "fr") map[:fr] greetings["fr"] greetings.get("fr") greetings["fr"] greetings.get("fr") [greetings objectForKey: @"fr"] $greetings["fr"] greetings["fr"] greetings["fr"] greetings["fr"] greetings["fr"]
Set Value greetings.insert(std::pair<string, string>("ru", "привет")); (assoc greetings "ru" "привет") Map.put(greetings, :ru, "привет") greetings["ru"] = "привет" greetings.put("ru", "привет") greetings["ru"] = "привет"; greetings.put("ru", "привет") [greetings setObject: @"привет" forKey: @"ru"]; $greetings["ru"] = "привет" greetings["ru"] = "привет" greetings["ru"] = "привет" greetings["ru"] = "привет" greetings["ru"] = "привет";
Remove Value greetings.erase("jp") (dissoc greetings "jp") Map.delete(greetings, "jp") delete(greetings, "jp") greetings.remove("jp") delete greetings["jp"]; greetings.remove("jp") [greetings removeObjectForKey: @"jp"]; unset($greetings["jp"]) del greetings["jp"] greetings.delete("jp") greetings["jp"] = nil delete greetings["jp"];
Enumerate Dictionary std::map<string,string>::iterator it = greetings.begin(); while (it != greetings.end()) { string key = it->first; string value = it->second; } (doseq [keyval greetings] (prn (key keyval) (val keyval))) Enum.map(greetings, fn {key, value} -> ... end) for key, value := range greetings { } for (Map.Entry<String, String> entry : greetings.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); } for (let key in greetings) { let value = greetings[key]; } for ((key, value) in greetings) { } for (NSString* key in greetings) { NSString *value = [greetings objectForKey: key]; ... } foreach ($greetings as $key => $value) { } for key, value in enumm.items(): ... greetings.each do |key, value| end for (key, value) in greetings { } for (let key in greetings) { let value: string = greetings[key]; }
Sort sort(colors.begin(), colors.end()); (sort colors) Enum.sort(colors) sort.Strings(colors) Collections.sort(colors) colors.sort() colors.sorted() [colors sortUsingSelector: @selector(compare:)]; sort($colors); colors.sort() colors.sort colors.sorted() colors.sort()
Sort using Function colors.sort([](string & s1, string & s2) { return s1.length() < s2.length(); }); (sort #(compare (count %1) (count %2)) colors) colors.sortBy { String.length } sort.Slice(colors, func(a, b string) bool { return len(names[a]) < len(names[b]) }) colors.stream() .sorted((String a, String b) -> a.length() - b.length()) .collect(Collectors.toList()) colors.sort((a, b) => a.length - b.length) colors.sortedBy { it.length } [colors sortArrayUsingComparator: ^(NSString *a, NSString *b) { return [a compare: b]; }]; function compare_length($a, $b) { return strlen($a) - strlen($b); } usort($colors, "compare_length"); colors.sort(key=len) colors.sort { |a, b| a.length <=> b.length } colors.sort_by { |c| c.length } colors.sorted { $0.count < $1.count } colors.sort((a: string, b: string): number => a.length - b.length)
Filter filter(colors, [](string s) { return s.length() == 6; }); (filter #(= (count %) 6) colors) Enum.filter(colors, fn(color) -> String.length(color) == 6 end) func Filter(sl []string, f func(string) bool) []string { sl2 := make([]string, 0) for _, v := range sl { if f(v) { sl2 = append(sl2, v) } } return sl2 } Filter(colors, func(v string) bool { return len(strings) == 6 }) colors.stream() .filter((String s) -> s.length() == 6) .collect(Collectors.toList()) colors.filter((c) => c.length == 6) colors.filter { color -> color.length == 6 } - (NSArray *) filter:(BOOL(^)(id object))block { NSMutableArray *result = [NSMutableArray array]; for (id object in self) { if (block(object)) { [result addObject:object]; } } return result; } [colors filter:^BOOL(id value) { return [value length] == 6; }]; function filter_length($s) { return strlen($s) == 6; } array_filter($colors, "filter_length"); list(filter(lambda c: (len(c) == 6), colors)) colors.filter { |c| c.length == 6 } colors.filter { $0.count == 6 } colors.filter((c: string): boolean => c.length == 6)
Map map(colors, [](string s) { return s.length(); }); (map count colors) Enum.map(colors, String.length) func Map(sl []string, f func(string) int) []int { sl2 := make([]string, len(sl)) for i, v := range sl { sl2[i] = f(v) } return sl2 } Map(colors, len) colors.stream() .mapToInt(String::length) .collect(Collectors.toList()) colors.map((c) => c.length) colors.map { color -> color.length } - (NSArray *) map:(id(^)(id object))block { NSMutableArray *result = [NSMutableArray array]; for (id object in self) { [result addObject:block(object)]; } return result; } [colors map:^(NSString * string) { return [NSNumber numberWithInt: [string length]]; }]; array_map("len", $colors) 😡 map(lambda c: len(c), colors) colors.map { |c| c.length } colors.map { $0.count } colors.map((c: string): number => c.length)
Reduce accumulate(colors.begin(), colors.end(), 0, [&](int t, string c){ return t + c.length(); }); (reduce #(+ %1 (count %2)) colors) Enum.reduce(colors, 0, fn(c, t) -> String.length(c) + t end) -- colors.stream() .mapToInt(String::length) .reduce(0, Integer::sum) colors.reduce((total, color) => total + color.length, 0) colors.reduce { acc, it -> acc + it.length } - (id) reduce:(id(^)(id acc, id object))block initial:(id)initial { for (id object in self) { initial = block(initial, object); } return initial; } NSArray *numbers = @[@1, @2, @3, @4, @5]; [numbers reduce:^id(id acc, id value) { return [NSNumber numberWithInt: [acc intValue] + [value intValue]]; } initial: @0] function add_length($total, $s) { return $total + strlen($s); } array_reduce($colors, "add_length", 0); reduce(lambda total, color: total + len(color), colors) colors.reduce(0) { |total, color| total + color.length } colors.reduce(0) { total, color in total + color.count } colors.reduce((total: number, color: string): number => total + color.length, 0)
Sum Numbers std::vector<int> numbers = { 1, 2, 3, 4, 5 }; accumulate(numbers.begin(), numbers.end(), 0, [&](int a, int b){ return a + b; }); (reduce + [1 2 3 4 5]) Enum.reduce([1, 2, 3, 4, 5], 0, fn(a, b) -> a + b end) total := 0 for _, n := range []int{1, 2, 3, 4, 5} { total += n } ArrayList<Integer> numbers = new ArrayList<>( Arrays.asList(1, 2, 3, 4, 5)); numbers.stream() .reduce(0, Integer::sum) [1, 2, 3, 4, 5].reduce((a, b) => a + b) val list = listOf(1, 2, 3, 4, 5) intList.reduce { acc, it -> acc + it } -- function add($a, $b) { return $a + $b; } array_reduce([1, 2, 3, 4, 5], "add"); reduce(lambda a, b: a + b, [1, 2, 3, 4, 5]) [1, 2, 3, 4, 5].reduce(0, :+) [1, 2, 3, 4, 5].reduce(0, +) [1, 2, 3, 4, 5].reduce((a: number, b: number): number => a + b)
Square Root sqrt(25) (Math/sqrt 25) :math.sqrt(x) math.Sqrt(25) Math.sqrt(25) Math.sqrt(25) sqrt(25) sqrt(25) sqrt(25) math.sqrt(25) Math.sqrt(25) 25.squareRoot() Math.sqrt(25)
Absolute Value abs(-99) (Math/abs -99) abs(-99) math.Abs(-99) Math.abs(-99) Math.abs(-99) abs(-99) abs(-99) abs(-99) abs(-99) -99.abs abs(-99) Math.abs(-99)
Factorial int factorial(int n) { return n > 0 ? n * factorial(n - 1) : 1; } (defn factorial [n] (reduce * (range 1 (inc n)))) defmodule Factorial do def of(0), do: 1 def of(n) when n > 0 do n * of(n-1) end end func Factorial(n int) int { if n > 0 { return n * Factorial(n - 1) } else { return 1 } } public static int factorial(int n) { return n > 0 ? n * factorial(n - 1) : 1; } function factorial(n) { return n > 0 ? n * factorial(n - 1) : 1; } fun factorial(n: Int): Int { return n > 0 ? n * factorial(n - 1) : 1 } int factorial(int n) { return n > 0 ? n * factorial(n - 1) : 1; } function factorial($n) { return $n > 0 ? $n * factorial($n - 1) : 1; } math.factorial(4) (1..n).inject(:*) || 1 func factorial(_ n: Int) -> Int { return n > 0 ? n * factorial(n - 1) : 1 } function factorial(n: number): number { return n > 0 ? n * factorial(n - 1) : 1; }
Swap Values swap(a, b); -- {a, b} = {b, a} a, b = b, a int temp = a; a = b; b = temp; [a, b] = [b, a] a = b.also { b = a } int temp = a; a = b; b = temp; $temp = $a; $a = $b; $b = $temp; a, b = b, a a, b = b, a (a, b) = (b, a) [a, b] = [b, a]
Round std::round(value * 100) / 100 (Double/parseDouble (format "%.2f" d))) Float.round(value, 2) float64(int64(value / .01)) * .01 Math.round(value * 100d) / 100d +(Math.round(value + "e+2") + "e-2") "%.2f".format(value).toDouble() round(100 * num) / 100 round($value, 2) round(value, 2) value.round(2) (value * 100).rounded() / 100 +(Math.round(value + "e+2") + "e-2")
Default Dict struct DefaultInt { int i = 0; }; std::map<int, DefaultInt> sizes; sizes[size]++; (update sizes size (fnil inc 0)) Map.put(sizes, size, Map.get(sizes, size, 0) + 1) sizes := make(map[int]int) sizes[size] = sizes[size] + 1 HashMap<Integer,Integer> sizes = new HashMap<>(); Integer count = sizes.getOrDefault(size, 0); sizes.put(size, count + 1); let sizes = {}; sizes[size] = (sizes[size] || 0) + 1; val count = sizes.get(size) ?: 0 sizes.put(size, count + 1) NSMutableArray *sizes = [NSMutableArray array]; NSNumber *count = [sizes objectForKey: size]; int val = count != nil ? [count intValue] : 0; count++; [sizes setObject: [NSNumber numberWithInt: count] forKey: size]; $sizes = array(); $size = array_key_exists($size, $sizes) ? $sizes[$size] : 0; $size++; $sizes[$size] = $size; sizes = defaultdict(int) sizes[size] += 1 sizes = Hash.new { |hash,key| hash[key] = 0 } sizes[size] += 1 var sizes = [Int:Int]() sizes[size, default: 0] += 1 let sizes = {}; sizes[size] = (sizes[size] || 0) + 1;
Array Equality std::vector<int> target = { 73, 77, 81, 87, 93, 99 }; if (missing == target) { std::cout << "Success!"; } (if (= missing [73 77 81 87 93 99]) (println "Success!")) target = [73,77,81,87,93,99] if length(missing -- target) == 0, do: IO.puts "\nSuccess!" target := []int{73, 77, 81, 87, 93, 99} if reflect.DeepEqual(missing, target) { fmt.Println("Success!") } if (missing.toString() .equals("[73, 77, 81, 87, 93, 99]")) { System.out.println("Success!"); } if (JSON.stringify(missing) === '[73,77,81,87,93,99]') { console.log('Success!'); } val missingString = missing.joinToString() if (missingString == "73, 77, 81, 87, 93, 99") { println("Success!") } if ([missing isEqualToArray: @[@73, @77, @81, @87, @93, @99]]) { NSLog(@"Success!"); } if ($missing) === [73, 77, 81, 87, 93, 99]) { echo('Success!'); } if missing == [73, 77, 81, 87, 93, 99]: print("Success!") if missing == [73,77,81,87,93,99] puts "Success!" end if missing == [73, 77, 81, 87, 93, 99] { print("Success!") } if (JSON.stringify(missing) === '[73,77,81,87,93,99]') { console.log('Success!'); }

Language similarity