RobertDober / lab42_more

More Functional Ruby.
MIT License
1 stars 0 forks source link

Memoization Support #1

Open RobertDober opened 9 years ago

RobertDober commented 9 years ago

Moved from lab42_core


Aiding the simple memoization pattern 

```ruby
     def fibo n
         @__fibo__ ||= {}
         @__fibo__[n] ||= n < 2 ? n : fibo(n-1) + fibo(n-2)
     end

declare like this

   def fibo n; n < 2 ? n : fibo(n-1) + fibo(n-2) end
   memoize :fibo # Can I find the param here in memoize?

Alternatively

   defmemo :fibo do |n|
      n < 2 ? n : fibo(n-1) + fibo(n-2) end
   end

Then we could have tools like

    clear_memo_cache :all
    clear_memo_cache for: :fibo
    #...
RobertDober commented 8 years ago

this is in core now