Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. 'use strict'
  2.  
  3. let memo = {}
  4.  
  5. class B {
  6. static f(n) {
  7. return n + 1
  8. }
  9. static use_memo = false
  10.  
  11. constructor(a, b) {
  12. this.a = a
  13. this.b = b
  14. }
  15.  
  16. toString() {
  17. return `B(${this.a.toString()},${this.b.toString()})`
  18. }
  19.  
  20. convert() {
  21. if(! B.use_memo) {
  22. return this.raw_convert()
  23. }
  24. if(! memo[this.toString()]) {
  25. memo[this.toString()] = this.raw_convert()
  26. }
  27. let next = memo[this.toString()]
  28. while(memo[next.toString()]) {
  29. memo[this.toString()] = memo[next.toString()]
  30. next = memo[this.toString()]
  31. }
  32. return memo[this.toString()]
  33. }
  34. raw_convert() {
  35. if(typeof(this.a) == 'number' && typeof(this.b) == 'number') {
  36. if(this.a == 0) {
  37. return B.f(this.b)
  38. }else if(this.b == 0){
  39. return new B(this.a-1, 1)
  40. }else{
  41. return new B(this.a-1, new B(this.a, this.b-1))
  42. }
  43. }else{
  44. return new B(typeof(this.a) == 'number' ? this.a : this.a.convert(),
  45. typeof(this.b) == 'number' ? this.b : this.b.convert())
  46. }
  47. }
  48. }
  49.  
  50. B.f = (n) => { return n + 1 }
  51. B.use_memo = true
  52. const g = (n) => new B(n, n)
  53.  
  54. let g3 = g(3)
  55. while(typeof(g3) != 'number') {
  56. console.log(g3.toString())
  57. g3 = g3.convert()
  58. }
  59. console.log(g3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement