Skip to content

Instantly share code, notes, and snippets.

View jbmilgrom's full-sized avatar

Jonathan Milgrom jbmilgrom

View GitHub Profile
@jbmilgrom
jbmilgrom / BankAccount.assignment.ts
Last active January 5, 2020 01:34
Highlight assignment of variable in class construction
class BankAccount {
private balance;
constructor(funds) {
this.balance = funds;
}
public withdraw(amount) {
this.balance = this.balance - amount; // <-- assign `this.balance` a new value
}
@jbmilgrom
jbmilgrom / functionComposition.higherOrder.js
Last active January 3, 2020 22:58
Example of function composition with higher-order function 'reduce'
const square = x => x * x;
const sum = (x, y) => x + y;
const sumOfSquares = (...nums) => nums.reduce((total, num) => sum(total, square(num)), 0);
sumOfSquares(1, 2, 3); // 14
const factorial = n => {
let total = 1;
while (n > 0) {
total = total * n;
n = n - 1;
}
return total;
};
factorial(0); // => 1
@jbmilgrom
jbmilgrom / factorial.iterative.functional.js
Last active December 24, 2019 20:27
Functional, iterative, factorial implementation
const factorial = n => {
const iterate = (total, i) => {
if (i === 0) return total;
return iterate(total * i, i - 1);
}
return iterate(1, n);
};
factorial(0); // => 1
factorial(1); // => 1
@jbmilgrom
jbmilgrom / BankAccount.imperative.js
Created November 29, 2019 21:04
Imperatively maintained "bank account"
let bankAccount = 100;
bankAccount = bankAccount - 20;
bankAccount; // 80
@jbmilgrom
jbmilgrom / BankAccount.assignment.js
Created November 29, 2019 18:16
Highlight assignment of variable in closure construction
const makeBankAccount = balance => ({
withdraw: amount => balance = balance - amount, // <-- assign `balance` a new value
checkBalance: () => balance
});
const bankAccount = makeBankAccount(100);
bankAccount.withdraw(20);
bankAccount.checkBalance(); // 80
@jbmilgrom
jbmilgrom / BankAccount.stateful.ts
Last active October 6, 2019 13:35
Subsequent calls to withdraw with the same argument produce different results
const bankAccount = new BankAccount(100);
bankAccount.checkBalance(); // 100
bankAccount.withdraw(20);
bankAccount.checkBalance(); // 80
@jbmilgrom
jbmilgrom / listOfStrings.immutable.js
Last active September 24, 2019 13:34
Arbitrary data can be the subject of functional behavior
const space = (w1, w2) => `${w1} ${w2}`;
const sentence = words => `${words.reduce(space)}.`;
sentence(['i', 'heart', 'functions']); // => "i heart functions."
@jbmilgrom
jbmilgrom / areAccountsEqual.mutable.ts
Last active September 23, 2019 16:48
Example of "sameness" in the context of mutability
/**
* With two _objects_, check equality by checking whether they are in fact the same object i.e. the reference.
*/
const areAccountsEqual = (a: BankAccount, b: BankAccount) => a === b;
const elainesAccount = new BankAccount(100);
const georgesAccount = new BankAccount(100);
elainesAccount.checkBalance(); // 100
georgesAccount.checkBalance(); // 100
@jbmilgrom
jbmilgrom / factorial.functional.js
Created September 23, 2019 16:42
Iterative process couched in terms of recursive procedure
const factorial = n => {
if (n === 0) return 1;
return n * factorial(n - 1);
};
factorial(1); // => 1
factorial(2); // => 2
factorial(3); // => 6