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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:59:26+00:00 2026-05-17T18:59:26+00:00

For caching purposes, I want to create an array, which maps input values of

  • 0

For caching purposes, I want to create an array, which maps input values of the function to output values. I know, that my function will be used only in this specific range, I think about something like this:

MyType = ... deriving (Ix)

myFunction :: MyType -> foo

myCache = createArrayFromFunction (start,end) myFunction

Is this possible or do I just think “not functional” and there is another solution. I need arrays, because I need O(1) access to the members and know the length from the beginning.

  • 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-17T18:59:26+00:00Added an answer on May 17, 2026 at 6:59 pm

    If you just want to create a cache, then you can just use listArray and map, as long as you have a list of all your indices:

    myCache :: Array MyType Foo
    myCache = listArray (start,end) . map myFunction $ range (start,end)
    

    I assumed that MyType has an Enum instance here; if it doesn’t, you’ll need some other way to generate a list of valid inputs, which depends on your type. As Reid Barton pointed out, this is what range is for.

    Another option, if you want to present a function to the user, would be

    myInternalFunc :: MyType -> Foo
    myInternalFunc mt = (complex calculation) (using mt)
    
    myFuncCache :: Array MyType Foo
    myFuncCache = listArray (start,end) . map myFunction $ range (start,end)
    
    myFunction :: MyType -> Foo
    myFunction = (myFuncCache !)
    

    Then you wouldn’t export myInternalFunc from your module; you probably wouldn’t export myFuncCache either, but I could imagine needing it. If you aren’t in a module, you could put myInternalFunc in a let– or where-block within myFuncCache. Once you do this, myFunction mt just does a cache lookup, and so is O(1).

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

Sidebar

Related Questions

Most will recommend caching as a list. I know that is a solution and
Anyone know of a good memory/disk caching library for AIR? Extra points for being
I have a listing of names, that I want to store in mysql as
I'm aware that serializing is used to convert data types into a storable format,
I am writing tests against our Caching mechanism and I want to be sure
Will the Hibernate caching(1st, 2nd, or Query) work when I use JdbcTemplate? I got
I need to use caching in my project. The problem is that, When page
With the new caching options in Rails 2.1 i get nice entires in my
I'm working with caching and persistence for the first time. I have a user
Is there a Winform caching library out there? I need to pass a few

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.