Skip to content

Instantly share code, notes, and snippets.

@hanabokuro
Last active August 9, 2019 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hanabokuro/b16e6371d5552379944eca495ffcc8d2 to your computer and use it in GitHub Desktop.
Save hanabokuro/b16e6371d5552379944eca495ffcc8d2 to your computer and use it in GitHub Desktop.
g(3)
'use strict'
let memo = {}
class B {
static f(n) {
return n + 1
}
static use_memo = false
constructor(a, b) {
this.a = a
this.b = b
}
toString() {
return `B(${this.a.toString()},${this.b.toString()})`
}
convert() {
if(! B.use_memo) {
return this.raw_convert()
}
if(! memo[this.toString()]) {
memo[this.toString()] = this.raw_convert()
}
let next = memo[this.toString()]
while(memo[next.toString()]) {
memo[this.toString()] = memo[next.toString()]
next = memo[this.toString()]
}
return memo[this.toString()]
}
raw_convert() {
if(typeof(this.a) == 'number' && typeof(this.b) == 'number') {
if(this.a == 0) {
return B.f(this.b)
}else if(this.b == 0){
return new B(this.a-1, 1)
}else{
return new B(this.a-1, new B(this.a, this.b-1))
}
}else{
return new B(typeof(this.a) == 'number' ? this.a : this.a.convert(),
typeof(this.b) == 'number' ? this.b : this.b.convert())
}
}
}
B.f = (n) => { return n + 1 }
B.use_memo = true
const g = (n) => new B(n, n)
let g3 = g(3)
while(typeof(g3) != 'number') {
console.log(g3.toString())
g3 = g3.convert()
}
console.log(g3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment