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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:58:39+00:00 2026-05-18T23:58:39+00:00

I just go through various documentation on Clojure concurrency and came accross the example

  • 0

I just go through various documentation on Clojure concurrency and came accross the example on the website (http://clojure.org/concurrent_programming).

(import '(java.util.concurrent Executors))
(defn test-stm [nitems nthreads niters]
(let [refs  (map ref (replicate nitems 0))
      pool  (Executors/newFixedThreadPool nthreads)
      tasks (map (fn [t]
                   (fn []
                     (dotimes [n niters]
                       (dosync
                         (doseq [r refs]
                           (alter r + 1 t))))))
                (range nthreads))]
(doseq [future (.invokeAll pool tasks)]
  (.get future))
(.shutdown pool)
(map deref refs)))

I understand what it does and how it works, but I don’t get why the second anonymous function fn[] is needed?

Many thanks,

dusha.

P.S. Without this second fn [] I get NullPointerException.

  • 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-18T23:58:40+00:00Added an answer on May 18, 2026 at 11:58 pm

    Here is a classic example of using higher-order functions:

    ;; a function returns another function
    (defn make-multiplyer [times]
      (fn [x]
        (* x times)))
    
    ;; now we bind returned function to a symbol to use it later
    (def multiply-by-two (make-multiplyer 2))
    
    ;; let's use it
    (multiply-by-two 100)   ; => 200
    

    In that code sample fn inside fn works the same way. When map invokes (fn [t] (fn [] …)) it gets inner fn.

    (def list-of-funcs (map (fn [t]
                              (fn [] (* t 10)))   ; main part
                            (range 5)))
    ;; Nearly same as
    ;; (def list-of-funcs (list (fn [] (* 0 10))
    ;;                          (fn [] (* 1 10))
    ;;                          ...
    ;;                          (fn [] (* 4 10))))
    
    
    (for [i list-of-funcs]
      (i))
    ; => (0 10 20 30 40)
    

    Update: And as Alex said tasks in the code sample is bound to list of callables which is passed then to .invokeAll().

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

Sidebar

Related Questions

I was just reading through Learning Python by Mark Lutz and came across this
I am going through the various Google AppEngine tutorials sometimes, and I just noticed
Just flicking through one of my favourite books (Ellen Ullman's The Bug) and there
I just got through manually removing the bin\ and obj\ folders from around 30
I just went through some MVC tutorials after checking this site out for a
I just worked through a book on WCF and was surprised that it didn't
I was just looking through some information about Google's protocol buffers data interchange format.
I've gone through just about every property I can think of, but haven't found
I was just browsing through questions on stack overflow, and i've come up to
Whats the normal procedure of clearing a form after POST? Just loop through the

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.