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

  • SEARCH
  • Home
  • 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 1055703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:36:16+00:00 2026-05-16T17:36:16+00:00

Each time a function is called, if it’s result for a given set of

  • 0

Each time a function is called, if it’s result for a given set of argument values is not yet memoized I’d like to put the result into an in-memory table. One column is meant to store a result, others to store arguments values.

How do I best implement this? Arguments are of diverse types, including some enums.

In C# I’d generally use DataTable. Is there an equivalent in Scala?

  • 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-16T17:36:17+00:00Added an answer on May 16, 2026 at 5:36 pm

    You could use a mutable.Map[TupleN[A1, A2, ..., AN], R] , or if memory is a concern, a WeakHashMap[1]. The definitions below (built on the memoization code from michid’s blog) allow you to easily memoize functions with multiple arguments. For example:

    import Memoize._
    
    def reallySlowFn(i: Int, s: String): Int = {
       Thread.sleep(3000)
       i + s.length
    }
    
    val memoizedSlowFn = memoize(reallySlowFn _)
    memoizedSlowFn(1, "abc") // returns 4 after about 3 seconds
    memoizedSlowFn(1, "abc") // returns 4 almost instantly
    

    Definitions:

    /**
     * A memoized unary function.
     *
     * @param f A unary function to memoize
     * @param [T] the argument type
     * @param [R] the return type
     */
    class Memoize1[-T, +R](f: T => R) extends (T => R) {
       import scala.collection.mutable
       // map that stores (argument, result) pairs
       private[this] val vals = mutable.Map.empty[T, R]
    
       // Given an argument x, 
       //   If vals contains x return vals(x).
       //   Otherwise, update vals so that vals(x) == f(x) and return f(x).
       def apply(x: T): R = vals getOrElseUpdate (x, f(x))
    }
    
    object Memoize {
       /**
        * Memoize a unary (single-argument) function.
        *
        * @param f the unary function to memoize
        */
       def memoize[T, R](f: T => R): (T => R) = new Memoize1(f)
    
       /**
        * Memoize a binary (two-argument) function.
        * 
        * @param f the binary function to memoize
        * 
        * This works by turning a function that takes two arguments of type
        * T1 and T2 into a function that takes a single argument of type 
        * (T1, T2), memoizing that "tupled" function, then "untupling" the
        * memoized function.
        */
       def memoize[T1, T2, R](f: (T1, T2) => R): ((T1, T2) => R) = 
          Function.untupled(memoize(f.tupled))
    
       /**
        * Memoize a ternary (three-argument) function.
        *
        * @param f the ternary function to memoize
        */
       def memoize[T1, T2, T3, R](f: (T1, T2, T3) => R): ((T1, T2, T3) => R) =
          Function.untupled(memoize(f.tupled))
    
       // ... more memoize methods for higher-arity functions ...
    
       /**
        * Fixed-point combinator (for memoizing recursive functions).
        */
       def Y[T, R](f: (T => R) => T => R): (T => R) = {
          lazy val yf: (T => R) = memoize(f(yf)(_))
          yf
       }
    }
    

    The fixed-point combinator (Memoize.Y) makes it possible to memoize recursive functions:

    val fib: BigInt => BigInt = {                         
       def fibRec(f: BigInt => BigInt)(n: BigInt): BigInt = {
          if (n == 0) 1 
          else if (n == 1) 1 
          else (f(n-1) + f(n-2))                           
       }                                                     
       Memoize.Y(fibRec)
    }
    

    [1] WeakHashMap does not work well as a cache. See http://www.codeinstructions.com/2008/09/weakhashmap-is-not-cache-understanding.html and this related question.

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

Sidebar

Related Questions

I've got a pickerview. I would like to call a specific function each time
I would like to make a ListBox function like a Grid . Each time
I want to create a function such that each time the function runs, the
I got a function which displays content based on menu options, each time a
I want to create stored procedure that call recursion function, each time it will
I have numerous functions (unknown at design time) that each take a specific number
Each time I use Merge() I have the following: 'Cannot implicitly convert type 'void'
Each time I add in the correct code, it gives me the same error
Each time my webpage is loaded it runs a routine in the page_load event
Each time I click on the map a new marker is placed on the

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.