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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:47:15+00:00 2026-06-15T00:47:15+00:00

I am fairly new to Clojure and functional programming in general and I’ve been

  • 0

I am fairly new to Clojure and functional programming in general and I’ve been struggling with the following problem. I’d like to assign a unique and stable index to a series of tokens (strings). Since there will be a lot more lookups than insertions, a hash-map seemed to be the way to go.

In Java I would’ve written something along the lines of

int last = 0; 
HashMap<String, Integer> lut = new HashMap<String, Integer>();

function Integer getIndex(String token) {
   Integer index = lut.get(token); 
   if(index == null) 
      last++;
      lut.put(token, last);
      return last;
    else { 
      return index;
    }
}

The transliterated version in Clojure would be something like

(def last-index (atom 0))
(def lookup-table (atom {}))

(defn get-index [token]
  (if (nil? (get @lookup-table token))
    (do 
      (swap! last-index inc)
      (swap! lookup-table assoc token @last-index)
      @last-index)
    (get @lookup-table token)))

But this doesn’t seem to be very idomatic since it basically side-effects and doesn´t even hide it.

So how would you do this without having the two atoms for keeping state?

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

    The answer given by Ankur is not thread safe, although I don’t think seh’s description of why is very helpful, and his alternatives are worse. It’s reasonable to say “Well I’m not worried about multiple threads now”, in which case that answer is fine. But it’s valuable to be able to write such things safely even if you don’t need that guarantee in any particular instance, and the only safe way is to do all your logic inside the swap!, like so:

    (let [m (atom {})]
      (defn get-index [token]
        (get (swap! m
                    #(assoc % token (or (% token) (count %))))
             token)))
    

    You can speed this up a bit by avoiding a swap! if there is already an entry when the function is called, and by avoiding an assoc if there is already an entry once you’ve entered the swap!, but you must “double check” that the map doesn’t have an entry for the current token before just assigning it (count %), because some other thread may have snuck in before you started swap!ing (but after you decided to swap!), and assigned a value for the current token, in which case you must respect that assignment instead of making a new one.

    Edit: as an aside, the Java version of course has the same thread-safety problem, because by default everything in Java is mutable and not thread-safe. At least in Clojure you have to put a ! in there, saying “Yes, I know this is dangerous, I know what I’m doing.”

    So in some sense Ankur’s solution is a perfect translation of the Java code, but even better would be to improve it!

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

Sidebar

Related Questions

Fairly new to socket programming, but I've been assigned with a whopper of project.
I'm fairly new to clojure but I've been having trouble finding good resources and
Fairly new to programming. I just can't wrap my head around how to get
Being fairly new to programming, I am having trouble understanding exactly what Homebrew does...
I'm fairly new to ruby and I've got a hash that looks like so:
I'm fairly new to C++ standard library and have been using standard library lists
In fairly new to MVC and I would like to use a session. I
I'm new to Clojure, so I've been going through the Clojure Koans the last
I've been playing a bit with Clojure and so far is fairly impressed, but
Fairly new to sqlite (and sql). For each row in a table, I'd like

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.