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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:54:09+00:00 2026-05-27T00:54:09+00:00

Ok so in C# I could write: public class Memorizer<K,TRes> { private Dictionary<K,TRes> _mem;

  • 0

Ok so in C# I could write:

public class Memorizer<K,TRes>
{
    private Dictionary<K,TRes> _mem;
    private Func<K,TRes> _function

    public Memorizer (Func<K,TRes> function)
    {
        _function = function;
        _mem= new Dictionary<K,TRes>();
    }

    public TRes Call(K arg)
    {
        if (mem.ContainsKey(arg)
        {
            return _mem[arg];
        }
        else
        {
            TRes ret=_function(arg);
            _mem[arg] = ret;
            return ret;
        }
    }
}

Which could be made use of for obvious gains:

public class FactorialCalculator()
{
    private Memorizer<ushort, ulong> _memorizedFactorial;
    public FactorialCalculator()
    {
        _memorizedFactorial = new Memorizer<ushort, ulong> (innerFactorial);
    }

    private ulong innerFactorial(ushort x)
    {
        return (x=0) ? 1 : x*Factorial(x-1)
    }

    public ulong factorial(ushort x)
    {
        _memorizedFactorial.Call(x);
    }

}

I’m sure it could be made more general and elegant.
And I know I’ll have overflow exceptions if x>20.
(And I may have typecast errors in there too)
BUt hopefully I made my point: i can create a class that can furful the needs for memoisation of pure mathematical functions (I.e. deterministic, side-effect free functions)
and get wonderful performance gains.

How can I accomplish a similar thing in Java?

  • 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-27T00:54:10+00:00Added an answer on May 27, 2026 at 12:54 am

    You can’t pass functions as data types in java. To fix this, use an interface.

    public interface ReturnFunction<K, V> {
        public V getValue(K k);
    }
    

    Now you can set innerFactorial to a data type.

    public ReturnFunction<Short, Long> innerFactorial = new ReturnFunction<Short, Long>(){
        public Long getValue(Short x){
            return (x=0) ? 1 : x*Factorial(x-1);
        }
    };
    

    This lets you pass innerFactorial as a data type:

    _memoizedFactorial = new Memorizer<Short, Long> (innerFactorial);
    

    And to call the function you write this:

    long someLong = _memoizedFactorial.getValue(someShort);
    

    Also, in Java don’t capitalize field or method names. It’s not standard and makes the code harder to read.

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

Sidebar

Related Questions

I could write myself a helper class that does this when given a functor,
If you have a C function which returns an integer, you could write a
I've got following classes: public class Container { private String name; private Data data;
A typical JPA entity looks like: @Entity public class Person { @Id private int
I know I could write scripts and create jobs to run them, but at
I wonder if I could write a program in the C -programming language that
Sure I could write this myself, but before I go reinventing the wheel is
Is there a piece of code I could write to be notified when a
If it said oneword, then I could write #oneword, but what do I write
I've found ScriptingJsonSerializationSection but I'm not sure how to use it. I could write

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.