Giter Club home page Giter Club logo

memoizable's Introduction

Memoizable

Introduction

When doing recursion sometimes we are faced with the problem of performance versus the beauty of the code we are implementing, specially when, due to the recursive nature of the method we have written we seem to be computing over and over the same.

The concept of memoization comes from the idea of capturing a certain method call and saving it’s result to an internal cache, so that, if this particular method (with the same parameters) is called, it will return the previously computed result.

I was inspired in writing this little module after reading an article on James Edward Grey II’2 blog (blog.grayproductions.net/articles/caching_and_memoization)

Example

Imagine we want to compute the Fibonacci sequence:

class Fibonacci
  def fib(num)
    return num if num < 2
    fib(num -1) + fib(num - 2)
  end
end

As you can see immediately is that this will result in poor performance the higher the number in the sequence we request as the algorithm will compute over and over same method calls.

You could add some cache functionallity into your method and class, although you would add new behaviour that the class and method shouldn’t really have as they should be dealing exclusively with the logic they are supposed to execute; in this case the computation of the Fibonacci sequence.

Here is how the class would look like when we Memonize it:

class Fibonacci
  include Memoizable

  def fib(num)
    return num if num < 2
    fib(num -1) + fib(num - 2)
  end

  memoize :fib
end

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but

    bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Copyright © 2009 Enrique Comba Riepenhausen. See LICENSE for details.

memoizable's People

Contributors

jeffwelling avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

wnight

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.