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.imperative.js
Created Nov 29, 2019
Imperatively maintained "bank account"
View BankAccount.imperative.js
let bankAccount = 100;
bankAccount = bankAccount - 20;
bankAccount; // 80
@jbmilgrom
jbmilgrom / BankAccount.assignment.js
Created Nov 29, 2019
Highlight assignment of variable in closure construction
View BankAccount.assignment.js
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.assignment.ts
Created Nov 29, 2019
Highlight assignment of variable in class construction
View BankAccount.assignment.ts
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 / 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 / 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 / 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"
You can’t perform that action at this time.