Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 944903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:40:10+00:00 2026-05-15T22:40:10+00:00

Suppose you have a simple function, which can get quite expensive for large values:

  • 0

Suppose you have a simple function, which can get quite expensive for large values:

fact(0) -> 1;
fact(N) -> N * fact(N - 1).

Where can I find a simple example of caching (or memoizing) function values by using dets?

Any other way for easy memoization would be highly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:40:11+00:00Added an answer on May 15, 2026 at 10:40 pm

    The idea is that every time you ask for your heavy calculation, you immediately check in the cache if you’ve already evaluated it. If yes, you simply return the stored value. If not, you have to evaluate the new value and to store it, before returning it to the final user.

    A dict, rather than a dets table, could also work.

    (The following solution is untested)

    -module(cache_fact).
    
    -export([init/0, fact/1]).
    
    init() ->
        {ok, _} = dets:open_file(values, []).
    
    fact(N) ->
        case dets:lookup(values, N) of
          [] ->
            Result = do_fact(N), 
            dets:insert_new(values, {N, Result}),
            Result;
          [{N, Cached}] ->
            Cached
        end.
    
    do_fact(0) ->
        1;
    do_fact(N) ->
        N * do_fact(N-1).
    

    You might want to encapsulate the whole thing into an Erlang generic server. In the init function you should create the DETS table, the fact/1 function should represent your API and you should implement the logic in the handle_call functions.

    A nicer example could be writing a shortener service for URLs, cached.

    As suggested by @Zed, it would make sense to store the partial results as well to avoid further re-calculations. If this is the case:

    -module(cache_fact).
    
    -export([init/0, fact/1]).
    
    init() ->
        {ok, _} = dets:open_file(values, []).
    
    fact(0) ->
        1;
    fact(N) ->
        case dets:lookup(values, N) of
          [] ->
            Result = N * fact(N-1),
            dets:insert_new(values, {N, Result}),
            Result;
          [{N, Cached}] ->
            Cached
        end.
    

    Obviously, even if this helps for large numbers, you have to consider the additional cost of adding an entry to the lookup table for every step. Considering the reasons why the caching has been introduced (we assume the calculation is very heavy, so the lookup insertion time is insignificant), this should be perfectly fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you have a simple block of code like this: app.get('/', function(req, res){ res.send('Hello
suppose I have a simple container which have three element: <div> <span>hello world</span> <input
I have one simple app that suppose to get information from user and send
Suppose I have a simple model, such as Record: @Model public class Record {
Suppose you have a simple class like this: class foo{ private: int* mData; int
Suppose I have the following simple struct: struct Vector3 { double x; double y;
Suppose I have the following (trivially simple) base class: public class Simple { public
Lets suppose that I have the following simple query var q = from p
Suppose I have a list of things (numbers, to keep things simple here) and
I have a function which makes an AJAX request to a server and returns

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.