Skip to content

Instantly share code, notes, and snippets.

@thurt
Last active April 3, 2016 23:27
Show Gist options
  • Save thurt/ac5528347275c2236bdc106fb58d411d to your computer and use it in GitHub Desktop.
Save thurt/ac5528347275c2236bdc106fb58d411d to your computer and use it in GitHub Desktop.
an example of how to implement an immutable counter
(() => {
class Counter {
constructor(start_value) {
this.plus = this.plus(start_value)
}
plusFn(num, _num) {
var res = num + _num
this.plus = (_num) => {
return this.plusFn(res, _num)
}
return res
}
plus(num) {
return (_num) => {
return this.plusFn(num, _num)
}
}
}
var myCounter = new Counter(1)
console.log(myCounter.plus(1)) // 2
console.log(myCounter.plus(1)) // 3
console.log(myCounter.plus(3)) // 6
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment