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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:16:39+00:00 2026-06-11T00:16:39+00:00

Starting with Clojure I discovered a talk by Rich Hickey where he demonstrates some

  • 0

Starting with Clojure I discovered a talk by Rich Hickey where he demonstrates some of Clojure’s strengths on a basic Ant-Simulator.

Can this code still be considered as a good reference for Clojure? Especially the parts when he recursively sends off functions to agents to simulate a game loop.
Example:

(defn animation [x]
  (when b/running
    (send-off *agent* #'animation))
    (. panel (repaint))
  (. Thread (sleep defs/animation-sleep-ms))
  nil)

Edit:

I am not interested in the #' reader macro but more wether it is idiomatic/good Clojure to
recursively call a function on a agent or not.

  • 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-06-11T00:16:40+00:00Added an answer on June 11, 2026 at 12:16 am

    This snippet is current in Clojure 1.4. Is it idiomatic for a function to submit a task back to the agent that called it? Yes.

    Here is an example that uses a similar approach to recursively calculate a factorial:

    (defn fac [n limit total]
      (if (< n limit)
        (let [next-n (inc n)]
           (send-off *agent* fac limit (* total next-n)) 
           next-n)
         total))
    
     (def a (agent 1))
    
     (await (send-off a fac 5 1))
     ; => nil
     @a
     ;=> 120
    

    Update

    The above is a contrived example and actually not a good one, as there is a race condition between the various recursive send-off calls and the later await. There may be some send-off calls yet to be added to the agent’s task queue.

    I re-wrote the above as follows:

    (defn factorial-using-agent-recursive [x]
      (let [a (agent 1)]
        (letfn [(calc  [n limit total]
                   (if (< n limit)
                     (let [next-n (inc n)]
                       (send-off *agent* calc limit (* total next-n))
                       next-n)
                     total))]
          (await (send-off a calc x 1)))
        @a))
    

    and observed the following behavior:

    user=> (for [x (range 10)] (factorial-using-agent-recursive 5))
    (2 4 3 120 2 120 120 120 120 2)
    user=> (for [x (range 10)] (factorial-using-agent-recursive 5))
    (2 2 2 3 2 2 3 2 120 2)
    user=> (for [x (range 10)] (factorial-using-agent-recursive 5))
    (120 120 120 120 120 120 120 120 120 120)
    

    Moral of the story is: don’t use agents for synchronous calculations. Use them for asynchronous independent tasks – like updating animations displayed to a user 🙂

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

Sidebar

Related Questions

Starting a new GWT application and wondering if I can get some advice from
This is where I'm starting to find static code analysis odd. Google Closure give
I'm just starting with Clojure and can't access to the doc function. I'm using
Starting with a recent new version of ADT, I've noticed this new attribute on
Starting with a list of objects containing two parameters notional and currency, how can
I am just starting out learning Clojure and Emacs. I have got Clojure Box
All, I'm starting to take a look at the Clojure language, and had a
I am writing some signal processing software, and I am starting off by writing
starting with priority_queue s, I have a problem like this: I need elements to
Starting Komodo IDE 7 on Ubuntu 11.10 crashes in libcrypto.so during startup (see this

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.