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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:13:36+00:00 2026-06-11T07:13:36+00:00

I’ve got a piece of code to implement a ref of a map of

  • 0

I’ve got a piece of code to implement a ref of a map of atoms containing maps

 > (def a (example.DynaRec.)

 > (dosync (assoc! a 3 {:id 3 :contents "stuff"} 4 {:id 4}))
 ;;=> #[DynaRec@1a659078: {4 [<Atom@118eb00c: {:id 4}] 3 [Atom@242110fc: {:id 3 :contents "stuff"}]]

I want to make change the way this is displayed in the repl so that it only outputs

> a 
;;=> ({:id 3 :contents "stuff"}
;;    {:id 4})

how can this be done? The code is shown below:

(ns example.dyna-rec
  (:gen-class
   :name example.DynaRec
   :prefix "-"
   :init init
   :state state
   :extends clojure.lang.AFn
   :implements [clojure.lang.IDeref
                clojure.lang.Seqable
                clojure.lang.ILookup
                clojure.lang.ITransientMap]))

(defn- $ [this] (:data (.state this)))

(defn- valid?
  ([e]
     (valid? e []))
  ([this e]
     (every? #(contains? e %) (:required (.state this)))))

(defn -deref [this] @($ this))

(defn -valAt
  ([this k] ((-deref this) k nil))
  ([this k nv] 
    (if-let [vatom ((-deref this) k nv)]
      @vatom)))

(defn -invoke
  ([this k] (-valAt this k))
  ([this k nv] -valAt this k nv))

(defn -seq [this] (seq (-deref this)))

(defn -count [this] (count (-deref this)))

(defn -without [this obj]
  (alter ($ this) dissoc obj))

(defn -assoc
  ([this obj] (-assoc this (:id obj) obj))
  ([this k v]
   {:pre [(valid? this v)
          (= k (:id v))]} 
    (alter ($ this) assoc k (atom v))
    this))

(defn -conj [this obj]
  (-assoc this obj))

(defn -persistent [this]
  (-deref this))

(defn -init []
  [[]  {:required  #{:id}
        :data      (ref {})}])
  • 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-11T07:13:38+00:00Added an answer on June 11, 2026 at 7:13 am

    To extend the classic java model (which clojure embraces) you can implement your own .toString which produces the correct strings, and then tell the REPL how to print this new class properly

    start by adding a super basic toString to dyna-rec.clj:

    (defn- -toString [this]
      (str (into {} this)))
    

    this can be called like this:

     (defn- -toString [this]
     (str (zipmap (keys this) (map deref (vals this)))))
    
    example.DynaRec> (.toString a)
    "{}"
    example.DynaRec> (str a)
    "{}"
    

    then improve the printer to match your desired output:

    (defn- -toString [this]
      (str (zipmap (keys this) (map deref (vals this)))))
    

    and test it:

    example.DynaRec> (str a)
    "{3 {:id 3, :contents \"stuff\"}, 4 {:id 4}}"
    example.DynaRec> 
    

    This still does not print correctly at the REPL, so we need to extend the multimethod used by the printer (print-method) which is used by the REPL to know about your new class:

    (defmethod print-method 
      example.DynaRec
      [this, w]
      (print-method (zipmap (keys this) (map deref (vals this))) w))
    

    this produces what you seek:

    example.DynaRec> a
    {3 {:id 3, :contents "stuff"}, 4 {:id 4}}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i got an object with contents of html markup in it, for example: string
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a view passing on information from a database: def serve_article(request, id): served_article
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.