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 7782781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:38:52+00:00 2026-06-01T19:38:52+00:00

Can anyone give me an example of underscore.js _.memoize() in action? Preferably using the

  • 0

Can anyone give me an example of underscore.js _.memoize() in action?

Preferably using the hashFunction, and even more preferably in coffeescript?

Here is a slightly modified version of that cute change counting function from SICP in coffeescript:

countChange = (amount)->

  cc = (amount, kindsOfCoins)->

    firstDenomination = (kindsOfCoins)->
      switch kindsOfCoins
        when 1 then 1
        when 2 then 5
        when 3 then 10
        when 4 then 25

    if amount is 0 then 1
    else if amount < 0 or kindsOfCoins is 0 then 0
    else 
      (cc amount, (kindsOfCoins - 1)) + 
      (cc (amount - firstDenomination(kindsOfCoins)), kindsOfCoins)

  cc amount*100, 4


console.log "Ways to make change for $0.85: " + countChange(.85)

How might I use underscore’s _.memoize() on that for example?

Many thanks in advance!

ps.. also, please don’t hesitate to shoot holes in the way that function was coded.. I’m very new to coffeescript and any help on making that code more idiomatic is welcome too.

  • 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-06-01T19:38:54+00:00Added an answer on June 1, 2026 at 7:38 pm

    One use of memoize here would be to reduce the number of calls to the inner cc function:

    n = 0
    countChange = (amount)->
      firstDenomination = (kindsOfCoins) ->
        [1, 5, 10, 25][kindsOfCoins - 1]
    
      cc = (amount, kindsOfCoins)->
        ++n # This is just a simple counter for demonstration purposes
        return 1 if amount is 0
        return 0 if amount < 0 or kindsOfCoins is 0
        (cc amount, (kindsOfCoins - 1)) +
          (cc (amount - firstDenomination(kindsOfCoins)), kindsOfCoins)
    
      cc = _.memoize cc, (a,k) -> "#{a},#{k}"
    
      cc amount*100, 4
    
    console.log "Ways to make change for $0.85: #{countChange(.85)}"
    ​console.log "#{n} iterations of cc"
    

    I’ve also rearranged things a little bit for compactness and I moved firstDenomination outside of cc to simplify cc while I was there; whether my ​​​​​​​​​​​​​​​​​​​​​​​​​​firstDenomination is better than yours is a matter of taste, I have a bias against using a switch to implement a simple lookup table but YMMV.

    The memoized version says “211 iterations of cc”, demo: http://jsfiddle.net/ambiguous/FZsJU/

    A non-memoized version says “8141 iterations of cc”, demo: http://jsfiddle.net/ambiguous/Xn944/

    So the non-memoized version calls cc ~40 times more often. The memoization may or may not be worthwhile depending on the computational overhead of the hashing function (mine is sufficient for demonstration purposes but not exactly optimized) and the overhead of the cache lookup. This is the standard question to ask when memoizing: is the caching faster than the cached computation?

    If we look at the implementation of _.memoize:

    // Memoize an expensive function by storing its results.
    _.memoize = function(func, hasher) {
      var memo = {};
      hasher || (hasher = _.identity);
      return function() {
        var key = hasher.apply(this, arguments);
        return _.has(memo, key) ? memo[key] : (memo[key] = func.apply(this, arguments));
      };
    };
    

    then you can see how it works and how the hasher is used. The memo object is used as a cache, and the hasher is used to convert the arguments of the memoized function to a key in memo; if we find the key then we can return the cached value immediately, otherwise we compute it the (presumably) slow way, cache it, and return it.

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

Sidebar

Related Questions

Can anyone tell or define more what is ancestor and give an example on
Can anyone give an example of the use of REF CURSOR from Oracle using
i am using django tagging. can anyone give any example as to how can
Can anyone give me an example for encrypt and decrypt an image using .net
Can anyone give me an example of how I can consume the following web
Does anyone use hashCode() anywhere? Can anyone give me an example of the exact
Can anyone give an example of a UDP Hole Punching ? Actually, I want
Can anyone give me an example of how to return the following json simply
Can anyone give me an example of the following desired OOP structure please. I
Can anyone give me an example of how to stream the output of an

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.