Instantly share code, notes, and snippets.

Jonathan Milgromjbmilgrom

• Sort options
Created Nov 29, 2019
Imperatively maintained "bank account"
View BankAccount.imperative.js
 let bankAccount = 100; bankAccount = bankAccount - 20; bankAccount; // 80
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
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
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
Created Sep 23, 2019
View factorial.imperative.js
 const factorial = n => { let f = 1; while (n > 0) { f = f * n; n = n - 1; } return f; }; factorial(1); // => 1
Last active Sep 23, 2019
View decrement100.mutable.js
 const decrement100 = x => { let r = 100; r = 100 - x; return r; }; decrement100(20); // => 80
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"]);
Last active Sep 22, 2019
"functional" semantics with looping construct