Skip to content

Instantly share code, notes, and snippets.

View jbmilgrom's full-sized avatar

Jonathan Milgrom jbmilgrom

View GitHub Profile
@jbmilgrom
jbmilgrom / profiles.immutable.ts
Last active August 18, 2019 15:25
Example of arbitrary immutable data
const profiles = [
{
lastName: "Curry",
firstName: "Steph",
team: "WARRIORS"
},
{
lastName: "James",
firstName: "Lebron",
team: "LAKERS",
@jbmilgrom
jbmilgrom / colors.immutable.ts
Created August 17, 2019 14:04
Example of arbitrary immutable data
enum PrimaryColor {
Red = "RED",
Yellow = "YELLOW",
Blue = "BLUE"
}
const green = [
{ color: PrimaryColor.Yellow, weight: 1 / 2 },
{ color: PrimaryColor.Blue, weight: 1 / 2 }
] as const;
@jbmilgrom
jbmilgrom / mutableNumber.invalid.js
Last active January 20, 2020 23:33
Imagined API for mutating numbers
const HOURS_IN_DAY = 24;
/**
* NOT VALID; imagined API for mutating numbers
*/
24.increment();
HOURS_IN_DAY === 25; // true
@jbmilgrom
jbmilgrom / decrementOneHundred.immutable.js
Last active August 16, 2019 18:17
Example of use of immutable binding
const oneHundred = 100; // <-- now a `const` instead of a `let`
const decrementOneHundred = x => oneHundred - x;
decrementOneHundred(20); // 80
// oneHundred = 80; <-- would be runtime error: "TypeError: Assignment to constant variable."
decrementOneHundred(20); // 80
@jbmilgrom
jbmilgrom / functionComposition.js
Created August 8, 2019 21:59
Example of function composition
const square = x => x * x;
const sum = (x, y) => x + y;
const sumOfSquares = (x, y, z) => sum(sum(square(x), square(y)), square(z));
sumOfSquares(1, 2, 3); // 14
@jbmilgrom
jbmilgrom / functionalProgram.js
Last active August 8, 2019 21:56
An example of a functional program
const square = x => x * x;
const sum = (x, y) => x + y;
// PSUEDO CODE
sum(sum(square(USER_INPUT_1), square(USER_INPUT_2)), square(USER_INPUT_3));
// REPL PSUEDO CODE
// > run functionalProgram.js with USER_INPUT_1=1, USER_INPUT_2=2, USER_INPUT_3=3,
// > 14
@jbmilgrom
jbmilgrom / BankAccount.defect.publicState.ts
Last active August 7, 2019 13:29
Defective "bank account" that exposes balance state publically
class BankAccount {
public balance; // <-- now public
constructor(funds) {
this.balance = funds;
}
public withdraw(amount) {
this.balance = this.balance - amount;
}
@jbmilgrom
jbmilgrom / BankAccount.defect.noState.ts
Last active August 7, 2019 13:32
Defective "bank account" that allows balance to be dictated to it by consumers
class BankAccount {
public withdraw(balance, amount) {
return balance - amount;
}
public checkBalance(balance) {
return balance;
}
}
function loginOrReloadAndGetPage(page: BasePage, username, password) {
page.load();
...
page.waitForInitLoaded();
...
if (!page.isLoggedIn()) {
...
page.waitForInitLoaded();
}
return page.waitForLoaded();
class BuyingFlow extends BasePage {
...
}
class BuyingFlow implements BasePageI {
load() {}
waitForInitloaded() {}
}
const BuyingFlow: BasePageI = {