Skip to content

Instantly share code, notes, and snippets.

Jonathan Milgrom jbmilgrom

Block or report user

Report or block jbmilgrom

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@jbmilgrom
jbmilgrom / BankAccount.stateful.ts
Last active Oct 6, 2019
Subsequent calls to withdraw with the same argument produce different results
View BankAccount.stateful.ts
const bankAccount = new BankAccount(100);
bankAccount.checkBalance(); // 100
bankAccount.withdraw(20);
bankAccount.checkBalance(); // 80
@jbmilgrom
jbmilgrom / listOfStrings.immutable.js
Last active Sep 24, 2019
Arbitrary data can be the subject of functional behavior
View listOfStrings.immutable.js
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 Sep 23, 2019
Example of "sameness" in the context of mutability
View areAccountsEqual.mutable.ts
/**
* 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 Sep 23, 2019
Iterative process couched in terms of recursive procedure
View factorial.functional.js
const factorial = n => {
if (n === 0) return 1;
return n * factorial(n - 1);
};
factorial(1); // => 1
factorial(2); // => 2
factorial(3); // => 6
View factorial.imperative.js
const factorial = n => {
let f = 1;
while (n > 0) {
f = f * n;
n = n - 1;
}
return f;
};
factorial(1); // => 1
View decrement100.mutable.js
const decrement100 = x => {
let r = 100;
r = 100 - x;
return r;
};
decrement100(20); // => 80
@jbmilgrom
jbmilgrom / buidCallbacks.js
Last active Sep 23, 2019
Problems with "internal" mutability
View buidCallbacks.js
const buildCallbacks = items => {
const callbacks = [];
let i;
for (i = 0; i < items.length; i++) {
callbacks.push(() => items[i]);
}
return callbacks;
}
const callbacks = buildCallbacks(["hello", "cruel", "world"]);
@jbmilgrom
jbmilgrom / add.imperative.js
Last active Sep 22, 2019
"functional" semantics with looping construct
View add.imperative.js
const add10 = n => {
let sum = n;
for (let i = 0; i < 10; i++) {
sum = sum + 1;
}
return sum;
};
add10(2); // => 12
add10(1); // => 11
@jbmilgrom
jbmilgrom / strings.immutable.js
Last active Sep 22, 2019
Arbitrary data can be the subject of functional behavior
View strings.immutable.js
const space = (w1, w2) => `${w1} ${w2}`;
space('hello', 'world'); // => "hello world"
@jbmilgrom
jbmilgrom / mutableNumber.invalid.js
Last active Sep 21, 2019
Imagined API for mutating numbers
View mutableNumber.invalid.js
const HOURS_IN_DAY = 24;
/**
* NOT VALID; imagined API for mutating numbers
*/
24.increment();
HOURS_IN_DAY + 1 === 26;
You can’t perform that action at this time.