Skip to content

Instantly share code, notes, and snippets.

View edwingustafson's full-sized avatar
💭
Upon Ye Olde Internetworke

Edwin Gustafson edwingustafson

💭
Upon Ye Olde Internetworke
  • Charlevoix, Michigan, USA
View GitHub Profile
@edwingustafson
edwingustafson / FizzBuzz.worksheet.sc
Last active April 6, 2020 15:19
FizzBuzz in Scala with no duplicated Strings or tests
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
@edwingustafson
edwingustafson / .bash_prompt
Last active May 11, 2018 18:03
Mercurial and Git repository bash prompt
# Default prompt
DEFAULT_PROMPT="\u@\h:\w$ "
# Separator
SEPARATOR=·
# Use the compiled front-end to Mercurial
HG=/usr/bin/chg
# Disable all Mercurial extensions
@edwingustafson
edwingustafson / fizzbuzz.js
Last active April 3, 2025 22:04
FizzBuzz is RxJS
#!/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)))
@edwingustafson
edwingustafson / type.ts
Last active May 21, 2018 16:12
TypeScript class/interface equivalent
/**
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
@edwingustafson
edwingustafson / factorial.ts
Last active May 24, 2018 01:46
Tail-recursive factorial in TypeScript
function factorial(
n: number,
accumulator: number = 1
): number {
if(n === 1)
return accumulator
return factorial(n - 1, n * accumulator)
}
@edwingustafson
edwingustafson / coinflip.js
Last active June 29, 2018 17:41
Condition assignment with and without variable bindings and logic
// conditionally assign variable binding
let coinflip1;
if( Math.random() > 0.5 ) {
coinflip1 = 'heads';
} else {
coinflip1 = 'tails';
}
@edwingustafson
edwingustafson / brand.ts
Last active July 24, 2018 13:48
Nominal typing with brands
// 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;
@edwingustafson
edwingustafson / flavor.ts
Created July 25, 2018 03:12
Nominal typing using flavors/flavoring
// 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';
@edwingustafson
edwingustafson / primetime.js
Last active July 26, 2018 19:00
Race between for loop (imperative) and lambda (functional)
const log = typeof console === 'object' ? console.log : print;
const time = (f) => {
const start = Date.now();
f.apply();
return Date.now() - start;
}
val middleInitial: Option[Char] = Some('Q');
val result: String = middleInitial match {
case Some(c) => s" ${c}."
case None => ""
}
println(s"John${result} Public");
// yields "John Q. Public"