Advertisement
Guest User

Refactoring using `get!`

a guest
Jan 21st, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 0.62 KB | None | 0 0
  1. #  get!(f::Function, collection, key)
  2. #
  3. #  Return the value stored for the given key, or if no mapping for the key is present, store key =>
  4. #  f(), and return f().
  5. #
  6. #  This is intended to be called using do block syntax:
  7. #
  8. #  get!(dict, key) do
  9. #      # default value calculated here
  10. #      time()
  11. #  end
  12.  
  13. # code from https://stackoverflow.com/q/54244661/1346276
  14.  
  15. function V2(t, K)
  16.     if t >= T
  17.         return 0.0
  18.     else
  19.         get!(V2dict, (t, K)) do
  20.             opt = optimize(K′ -> -(log(K - K′) + β * V2(t+1, K′)), eps(), K, iterations = 100_000)
  21.             Optim.minimum(opt)
  22.         end
  23.     end
  24. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement