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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:08:08+00:00 2026-05-28T00:08:08+00:00

I would like to redefine some functions in my program on startup according to

  • 0

I would like to redefine some functions in my program on startup according to some metadata on those functions.

I am new to clojure so I would like what is the idiomatic way to accomplish this.

What I would like to do is use a cache (like memcache) to cache results of some functions (database results). In similar way like memoize or contrib core.cache, but i would like to redefine original functions transparently to the rest of the program, according to metadata which defines the caching policy.

Java libraries usually use annotations and code generation to accomplish this. But I am wandering what is idiomatic clojure way of accomplishing this?

I have explored a few options on the internet but they don’t seem too satisfactory.
Binding is not what I want because it only works on the current thread.
Other options seems to be using some internal java functions which I would like to avoid, or
binding ns and rerefining functions with eval.

I understand that i can list the potential functions in one namespace with (keys (ns-publics ‘foo)), but have not explored yet how to list non public functions and how to list available namespaces (currently loaded?) – maybe there is namespace loading hook i can use?

EDIT:
This is a small example of what I have in mind. Wrap is a function that would perform caching according to origs metadata. Caching and metadata are absent from example, and both wrap and orig are in the same namespace.

(defn orig []
    "OK")
(defn orig2 []
    "RES 2")

(defn wrap [f & args]
    (let [res (apply f args)]
        println "wrap" f args "=" res
        res))

(set! orig (wrap orig))
(set! orig2 (wrap orig2))

After evaluating last two forms orig and orig2 should be redefined to use wrapped versions.
Unfortunately I get the following error in REPL:

java.lang.IllegalStateException: Can’t change/establish root binding of: orig with set (NO_SOURCE_FILE:0)

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

    You can use def/defn again and it will change the definition of the function (technically it will write a new definition which uses the same name).

    So you could just do:

    (def orig (wrap orig))
    (def orig2 (wrap orig2))
    

    Also, if I understand your intention: wrap should be returning a function, not a result.

    (defn wrap [f]
      (fn [& args]
        (let [res (apply f args)]
          (println "wrap" f args "=" res)
          res)))
    

    If you look at the memoize standard function it works in precisely this way.

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

Sidebar

Related Questions

I would like to redefine certain functions in PHP that are already built for
I have some objects which have been created automatically by linq2SQL. I would like
Is there any way to redefine a class or some of its methods without
http://jquery.malsup.com/cycle/pager7.html shows a way to use image thumbnail as pager. I would like to
I would like to provide all mathematical functions for the number-like objects created by
I would like to redefine a class in c++ (non clr). Here's the reason
Would like to get a list of advantages and disadvantages of using Stored Procedures.
Would like to create a strong password in C++. Any suggestions? I assume it
Would like to be able to set colors of headings and such, different font
Would like to know what a programmer should know to become a good at

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.