Last active
September 18, 2020 05:28
-
-
Save atomicpages/77573bb5ac5a66a199b864de70227fd6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def Counter(initial = 0) { | |
| def count = initial | |
| def inc = { count += it } | |
| return [ | |
| increment: { inc(1) }, | |
| decrement: { inc(-1) }, | |
| value: { count } | |
| ] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Counter(initial = 0) { | |
| let count = initial; | |
| const inc = n => count += n; | |
| return { | |
| increment: () => inc(1), | |
| decrement: () => inc(-1), | |
| value: () => count, | |
| }; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get(url, Closure cb) { | |
| sleep(1000); | |
| cb.call() // or just do cb() | |
| } | |
| get('https://dev.to', { print 'success!'}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function get(url, cb) { | |
| setTimeout(cb, 1000); | |
| } | |
| get('https://dev.to', () => console.log('success!')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for (def value in [1,3,4]) { | |
| println value | |
| } | |
| // java colon notation works, too | |
| for (def value : [1,3,4]) { | |
| println value | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for (const value of [1,3,4]) { | |
| console.log(value) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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} | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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} | |
| `; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const s = 'hello'; | |
| switch (s) { | |
| case 'hello': | |
| console.log('true'); | |
| // fall through | |
| case s.includes('llo'): | |
| console.log('true'); | |
| break; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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