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 log = typeof console === 'object' ? console.log : print; | |
const time = (f) => { | |
const start = Date.now(); | |
f.apply(); | |
return Date.now() - start; | |
} |
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
// https://spin.atomicobject.com/2018/01/15/typescript-flexible-nominal-typing/ | |
type Flavor<K, T> = K & { __flavor?: T }; | |
type CatalogNumber = Flavor<string, 'catalogNumber'>; | |
type CasNumber = Flavor<string, 'casNumber'>; | |
const pge2CatalogNumber: CatalogNumber = '14010'; | |
const pge2CasNumber: CasNumber = '363-24-6'; |
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
// Adapted from https://michalzalecki.com/nominal-typing-in-typescript/ | |
// see also "Flavoring" https://spin.atomicobject.com/2018/01/15/typescript-flexible-nominal-typing/ | |
type Brand<K, T> = K & { __brand: T }; | |
type CatalogNumber = Brand<string, 'catalogNumber'>; | |
type CasNumber = Brand<string, 'casNumber'>; | |
const pge2CatalogNumber: CatalogNumber = '14010' as CatalogNumber; |
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
// conditionally assign variable binding | |
let coinflip1; | |
if( Math.random() > 0.5 ) { | |
coinflip1 = 'heads'; | |
} else { | |
coinflip1 = 'tails'; | |
} | |
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 factorial( | |
n: number, | |
accumulator: number = 1 | |
): number { | |
if(n === 1) | |
return accumulator | |
return factorial(n - 1, n * accumulator) | |
} |
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
/** | |
Simplest type available in TypeScript, an interface with a couple of properties | |
*/ | |
interface Foo { | |
a: string, | |
b: number | |
} | |
/** | |
Equivalent simple class: public properties but no methods or private members |
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
#!/usr/bin/env node | |
const fizz = "Fizz"; | |
const buzz = "Buzz"; | |
const fizzbuzz = `${fizz}${buzz}`; | |
const Rx = require('rxjs/Rx'); | |
Rx.Observable.range(1,100) | |
.map(n => n % 15 === 0 ? fizzbuzz : (n % 3 === 0 ? fizz : (n % 5 === 0 ? buzz : n))) |
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
# Default prompt | |
DEFAULT_PROMPT="\u@\h:\w$ " | |
# Separator | |
SEPARATOR=· | |
# Use the compiled front-end to Mercurial | |
HG=/usr/bin/chg | |
# Disable all Mercurial extensions |
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
val fizz = "Fizz" | |
val buzz = "Buzz" | |
val fizzBuzz = s"${fizz}${buzz}" | |
1 to 100 map { n => | |
(n % 3, n % 5) match { | |
case (0, 0) => fizzBuzz | |
case (0, _) => fizz | |
case (_, 0) => buzz | |
case _ => n |
NewerOlder