Guest User

Untitled

a guest
Jul 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #fib with memoization
  2. fakeClosure := Object clone
  3. fakeClosure memo := Map clone
  4. fakeClosure fibmemo := method(num,
  5. #I need this next line when i assign this method to a slot in the
  6. #outer object (see last line of this gist).
  7. #Feels like a 'this/self' binding issue. Otherwise i would have expected
  8. #the memo slot on fakeClosure to be visible here
  9. memo := fakeClosure memo #not strictly necessary when calling 'fakeClosure fibmemo'
  10. memo atPut(0 asString, 0)
  11. memo atPut(1 asString, 1)
  12. fibhelp := method(num,
  13. one := memo at((num-1) asString)
  14. two := memo at((num-2) asString)
  15. if(one == nil
  16. , one = fibhelp(num-1,memo)
  17. memo atPut((num-1) asString, one)
  18. )
  19. if(two == nil
  20. , two = fibhelp(num-2,memo)
  21. memo atPut((num-2) asString, two)
  22. )
  23. one + two
  24. )
  25. fibhelp(num,memo)
  26. )
  27. #not really necessary, but i wanted it to be uniform with the other calls
  28. #makes line 9 necessary
  29. fibmemo := fakeClosure getSlot("fibmemo")
Add Comment
Please, Sign In to add comment