Skip to content

Instantly share code, notes, and snippets.

@atomicpages
Last active September 18, 2020 05:28
Show Gist options
  • Save atomicpages/77573bb5ac5a66a199b864de70227fd6 to your computer and use it in GitHub Desktop.
Save atomicpages/77573bb5ac5a66a199b864de70227fd6 to your computer and use it in GitHub Desktop.
def Counter(initial = 0) {
def count = initial
def inc = { count += it }
return [
increment: { inc(1) },
decrement: { inc(-1) },
value: { count }
]
}
function Counter(initial = 0) {
let count = initial;
const inc = n => count += n;
return {
increment: () => inc(1),
decrement: () => inc(-1),
value: () => count,
};
}
def get(url, Closure cb) {
sleep(1000);
cb.call() // or just do cb()
}
get('https://dev.to', { print 'success!'})
function get(url, cb) {
setTimeout(cb, 1000);
}
get('https://dev.to', () => console.log('success!'));
for (def i = 0; i < 10; i++) {
println i
}
// multiple inline assignments
for (def i = 0, j = 1; i < 10; i++) {
println "${i} ${j}"
}
// multiple assignments
for (def (i, j) = [0, 1]; i < 10; i++) {
println "${i} ${j}"
}
for (let i = 0; i < 10; i++) {
console.log(i);
}
// multiple assignment
for (let i = 0, j = 1; i < 10; i++) {
console.log(i, j);
}
// destructured assignments
for (let [i, j] = [0, 1]; i < 10; i++) {
console.log(i, j);
}
for (def value in [1,3,4]) {
println value
}
// java colon notation works, too
for (def value : [1,3,4]) {
println value
}
for (const value of [1,3,4]) {
console.log(value)
}
def age = 29
def exp = 10 ** 3
def binary = 0b1101
def octal = 0755 // starts with 0
def hex = 0x0AB1
def bigint = 1234g // use g suffix
def n = 1_000_000_000
const age = 29;
const exp = 10 ** 3; // @babel/plugin-transform-exponentiation-operator
const binary = 0b1101;
const octal = 0o755;
const hex = 0x0AB1;
const bigint = 1234n;
const n = 1_000_000_000; // @babel/plugin-proposal-numeric-separator
def s = 'Simple string'
def interpolated = "Interpolated ${string}"
def multi = '''This is a multi
line string'''
def multi_alt = '''
This string obeys
white space
rules
'''
def mutli_int = """
This is a multi-line
string that is ${interpolated}
"""
let s = 'Simple string';
let interpolated = `Interpolated ${string}`;
let multi = 'This is a multi \
line string';
let multi_alt = `
This string obeys
white space
rules
`;
let mutli_int = `
This is a multi-line
string that is ${interpolated}
`;
def s = 'hello'
switch (s) {
case 'hello':
println true
// fall through
case s.contains('llo'):
println true
break
}
def a = [1,2,3]
switch (a) {
case [1,2,3]: // matches against primitive arrays
println true
}
def x = 120
switch (x) {
case 100..200: // specify a range
println true
case { x < 200 }: // pass a lambda expression
println 'small'
}
const s = 'hello';
switch (s) {
case 'hello':
console.log('true');
// fall through
case s.includes('llo'):
console.log('true');
break;
}
def foo = 123 // preferred
var foo = 123
// define types using java-like initializers
int foo = 123
final int foo = 123
// multiple (or destructured) assignments
def (a, b, c) = [1, 2, 3]
let foo = 123;
let foo = 123;
// define types using typescript (or flow)
let foo: number = 123;
const foo = 123;
// destructured assignments
const [a, b, c] = [1, 2, 3];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment