Multi-Language Cheat Sheet
String Interpolation
C♯
$”{name} is {age:f1} years old”;
Python
f’You have {elves} elves’ -> You have 5 elves (assuming the variable elves = 5)
JavaScript
You have ${elves} elves
Regular Expressions
Scala
val re = "([\\w ]+)".r
val m = re.findFirstMatchIn(line).get
"(\\d+)-(\\d+)".r.findAllMatchIn(line)
Ruby
x =~ /text/
has_separator = /[ -]/.match?(value)
Python
import re
match = re.search(<regex>, <data>)
match.group(<num>)
Java
import java.util.regex.Pattern;
var p = Pattern.compile("(\\d+)");
var m = p.matcher(line);
m.matches();
var num1 = Integer.parseInt(m.group(1));
Rust
use regex::Regex;
let r1 = Regex::new(r"(\d+)").unwrap();
for captures in r1.captures(&line) {
let cmd = &captures[1];
}
Collection Range
Ruby
c[0..2]
Loop over a range of values
Java
range(0, 10).forEach(n -> {});
Ruby
(40..90).each do |note|
Iterate
JavaScript
for (let val of it)
corners.forEach((coords, i) => {…})
C♯
foreach (var item in items)
Create an array of things
JavaScript
Array.from({length: 5}, (v, i) => i);
Checkbox Checked
jQuery
$(…).is(':checked')
Format to some number of decimal places
Python
f'{1.234:.1f}'
Java
System.out.printf("i: %d, sum: %d\n", i, sum);
Remove from end of array
JavaScript
a.pop()
Add to front of array
JavaScript
a.unshift(elem)
Add to end of array
JavaScript
push
Test a predicate against every element
JavaScript
[1,2,3].every(n => n === n))
Extract array from p5 vector
v.array()
3D z
p5.js
Increases coming towards you.
Change case
Ruby
'ABC'.downcase()
Python
'ABC'.lower()
Java
"ABC".toLowerCase()
Strip chars from string
Ruby
puts 'a.b'.tr('.', '')
Array element exists
Ruby
matches.any? {|u| u.email == email}
Scope
Ruby
Top, module, class, method (no special scope inside blocks like if
s)
Sets
Ruby
===
or include?
Python
s = set()
s.add(e)
in
XmlHttpRequest
CoffeeScript
fetch('/polled_events/searches')
.then((response) -> response.json())
.then((data) ->
console.log(data)
).catch((error) -> console.error(error))
Queues
Python
from queue import Queue
q = Queue()
q.put(e)
q.get()
q.empty()
Format real numbers
Python
f'result: {value:{width}.{precision}}'
print(f'Pi, rounded: {math.pi:.3f}')
Pi, rounded: 3.142
Java
System.out.printf("Pi, rounded: %.3f", PI);
Pi, rounded: 3.142
Localize numbers (add thousands separator)
TypeScript (Node.js)
num.toLocaleString()
Java
NumberFormat.getNumberInstance.format(value)
Convert to and from characters and ASCII codes
JavaScript
'a'.charCodeAt(0)
97
String.fromCharCode(97)
"a"
Language Setup
Node
npm install @types/node --save-dev