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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:56:13+00:00 2026-05-29T05:56:13+00:00

In Clojure if I memoize a function, name it f and call it on

  • 0

In Clojure if I memoize a function, name it f and call it on an argument a.

If a is a large lazy value, does memoize return a value based on matching the thunk as opposed to forcing the evaluation of a and matching on the result?

Where a thunk is the unevaluated part of the lazy sequence.

If this isn’t the case is there a built-in way to get this behaviour?

Thanks!

  • 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-29T05:56:14+00:00Added an answer on May 29, 2026 at 5:56 am

    As stated by mikera, memoize doesn’t handle infinite lazy seqs. I’m adding this answer to provide a short description of the implementation reasons for this (plus two ideas for identity-based memoization schemes, one simple, one more complex). (Edit: actually there is a simple general identity-based solution, see below.)

    Why it doesn’t work

    memoize uses a hash map to store the mapping from arguments to values and hash maps use clojure.lang.Util/hasheq when determining if an object is one of their keys (except empty maps which just return false). Since hasheq‘s implementation for lazy seqs forces the entirety of the seq, asking any map if an infinite lazy seq is one of its keys will cause it to go into an infinite, memory-exhausting loop. Thus the same goes for memoize.

    Strictly speaking, initially the map is an array map. (In Clojure, for reasons of efficiency, small maps are usually array maps; if enough items are assoc‘d onto an array map, the return value becomes a hash map.) However non-empty array maps also fail to handle infinite lazy seqs due to a similar reason involving an equivalence-checking method.

    A solution

    System/identityHashCode returns whatever hashCode would return for a given objects if it used the default implementation (whether or not its hashCode is overridden).

    (defprotocol PWrapped
      (-unwrap [this]))
    PWrapped
    
    (defn wrap [o]
      (reify
        Object
        (hashCode [_] (System/identityHashCode o))
        PWrapped
        (-unwrap [_] o)))
    
    ;;; adapted from clojure.core/memoize, (C) Rich Hickey, EPL-licenced
    (defn memoize
      "Returns a memoized version of a referentially transparent function. The
      memoized version of the function keeps a cache of the mapping from arguments
      to results and, when calls with the same arguments are repeated often, has
      higher performance at the expense of higher memory use."
      {:added "1.0"
       :static true}
      [f]
      (let [mem (atom {})]
        (fn [& args]
          (if-let [e (find @mem (map wrap args))]
            (val e)
            (let [ret (apply f args)]
              (swap! mem assoc (map wrap args) ret)
              ret)))))
    

    Now you can do this (which wouldn’t work with the regular memoize):

    user> (def r (lazy-cat (range 10000) (prn :foo) (range)))
    #'user/r
    user> (def f (memoize #(apply + (take 100 %))))
    #'user/f
    user> (f [1 2 3])
    6
    user> (f r)
    4950
    

    Original discussion of alternative implementations follows.

    I don’t think there is a built-in solution using pointer equality. To implement one as general as memoize, you’d have to implement a map structure using pointer-equality-based hashing (viz. System/identityHashCode). Or you could build a simple “most recently used” cache with clojure.lang.PersistentQueue.

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

Sidebar

Related Questions

Does Clojure specify the order of evaluation of function arguments? I.e. When I call
Is there anything in Clojure that is equivalent to Common Lisp's symbol-name function?
in clojure i have vector [myfn1 myfn2 myfn3] how can i call functions named
In clojure, I would like to create a record inside a function. I tried:
From Clojure it is easy enough to use Java libraries...but what libraries does Clojure
So following on from Clojure macro to create a synonym for a function ,
Clojure's split-with function is quite handy, but has to traverse the leading part of
Clojure macro noob here. I have a function with some optional parameters, e.g. (defn
Clojure has a -> macro which inserts each expression recursively as the first argument
Learning clojure, trying to create a lazy infinite sequence of all prime numbers. I'm

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.