GithubHelp home page GithubHelp logo

mimi's Introduction

Hex.pm Version

Mimi the mini memoizer.

Sometimes you want a simple way to store and retrieve past function values. Mimi can help!

Explanation and usage

Memoization is a technique for boosting performance of long-running idempotent functions. When a function is first called, the function is executed with the given arguments and the result is stored. When the same function is called again with the same arguments, the result is retrieved directly without executing the function.

Mimi has one function, mmemoize, that accepts an anonymous function with a single argument and returns a tuple that contains the memoized version of that function. As an example, memoize the long-running function

{:ok, pid, greet} = 
  fn name ->
    :timer.sleep(3_000)
    "Hello, #{name}!"
  end
  |> Mimi.memoize()

When greet.("world") is called the first time, it will run for about 3 seconds before returning "Hello, world!". When called a second time with the same argument, greet will return almost immediately with the same result.

Under the hood

Mimi uses an Agent to store a map from argument values to the returned result. Mimi.memoize is a function that starts the Agent process and returns a three-element tuple {:ok, pid, memoized_function}. The PID of the Agent is made available so that the process can be inspected or terminated with Agent.get or Agent.stop, respectively.

Notes

Disclaimer

The memoized state in Mimi isn't managed in any way; it will continue to grow until the parent process is terminated or until the Agent process is terminated manually. It is not advisable to use Mimi is critical applications.

Recursion

Mimi will happily memoize a recursive function, but only at the top level. It will not memoize the internal recursive calls. So, if you're trying to speed up a naively recursive Fibonacci generator, Mimi won't be of much help. It's certainly possible to write the function in a way that uses memoization, but it wouldn't be a simple wrapper. A memoized version of a naive recursive implementation of a generator for Fibonacci numbers might look like

fib = fn n ->
  {:ok, _, f_mem} = fn {f, n} ->
    if n <= 1 do
      n
    else
      f.({f, n - 2}) + f.({f, n - 1})
    end
  end
  |> Mimi.memoize()

  f_mem.({f_mem, n})
end

Without memoization, this function has exponentially time complexity. Good luck waiting around for the 100th Fibonacci number! With memoization, this function function returns in milliseconds. You'll see that fib.(100) yields 354224848179261915075.

mimi's People

Contributors

epfahl avatar

Stargazers

 avatar

Watchers

 avatar

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.