Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement