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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:25:22+00:00 2026-06-04T08:25:22+00:00

Is there a more idiomatic/readable way of writing a get-and-set function in Clojure than:

  • 0

Is there a more idiomatic/readable way of writing a get-and-set function in Clojure than:

(def the-ref (ref {}))

(defn get-and-set [new-value]
  (dosync
    (let [old-value @the-ref]
     (do
       (ref-set the-ref new-value)
       old-value))))
  • 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-04T08:25:24+00:00Added an answer on June 4, 2026 at 8:25 am

    for the simple cases I tend to see this operation used directly instead of wrapped in a function:

    hello.core> (dosync (some-work @the-ref) (ref-set the-ref 5))
    5
    

    In this case dosync generally serves as the wrapper you are looking for. within the dosync This is significant because dosync composes nicely with other transactions and makes the bounds of the transaction visible. If you are in a position where the wrapper function can completely encapsulate all the references to the ref then perhaps refs are not the best tool.

    A typical use of refs could look more along these lines:

    (dosync (some-work @the-ref @the-other-ref) (ref-set the-ref @the-other-ref))
    

    The need to wrap it is rare because when ref’s are used they typically are use in groups of more than one ref because coordinated changes are required by the problem at hand. In cases where their is just one value then atoms are more common.

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

Sidebar

Related Questions

Is there are more efficient way than the following for selecting the third parent?
is there a more simple way to get combobox (WinForms) selected items text? string
Is there a more concise and idiomatic way to write the following code, which
Is there a more idiomatic way of opening a resource in Scala and applying
Is there anything more idiomatic than the following? foo.class == String
Is there more elegant (less code) way of find a matrix OUT, with colSums(OUT)<=a
There are more than one solution for finding the-number-of-digits in a given number. For
Is there anything more compact (or otherwise better) than this? <x:Array x:Key=titles Type=System:String> <System:String>Mr.</System:String>
Is there a more railsy way to do this query in rails 3? scope
If there are more than one rows in my source that do not match

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.