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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:09:47+00:00 2026-05-12T17:09:47+00:00

Here is an overly simplistic example for illustration: I can encapsulate an implementation detail

  • 0

Here is an overly simplistic example for illustration:

I can encapsulate an implementation detail such as using an atom for a counter:

(defn make-counter
  ([] (make-counter 0))
  ([init-val]
   (let [c (atom init-val)]
     {:get (fn [] @c)
      :++ (fn [] (swap! c inc))})))

But that means I need to redefine everything to add a feature (no inheritance):

(defn make-bi-counter
  ([] (make-bi-counter 0))
  ([init-val]
   (let [c (atom init-val)]
     {:get (fn [] @c)
      :++ (fn [] (swap! c inc))
      :-- (fn [] (swap! c dec))})))

Whereas if it were possible to just extend the one function:

(assoc c :-- (env (:++ c) (fn [] (swap! c dec))))

(def c (make-counter))
(def b (make-bi-counter))
user=> ((:-- b))
-1
user=> ((:-- b))
-2
user=> ((:get b))
-2

Or I could have just exposed the atom and had independent functions:

(defn -- [a] (swap! a dec))
(def a (atom 0))
(-- a)

It appears the best option is to forgo encapsulation, if ‘inheritance’ (or perhaps more accurately: extension) is desirable.

  • 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-12T17:09:47+00:00Added an answer on May 12, 2026 at 5:09 pm

    Yes, I think idiomatic Clojure is to separate your data from your functions, for precisely the reason that you can later write new functions to work on the old data.

    Bundling functions with data also means you can’t alter your functions later without altering or re-generating all of your data structures, since you’ll have these anonymous functions stored all over the place. Developing interactively at a REPL, I’d hate to have to hunt down all of my data structures to fix them every time I change a function. Closures in hash-maps are clever but they’re pretty fragile and I wouldn’t go that route unless there was a really good reason.

    It only takes a modicum of discipline to define your interface (as functions) and then remember to stick to your interface and not to mess around with the atom directly. It’s unclear what benefit you’re going to get from forcibly hiding things from yourself.

    If you want inheritance, multimethods are a good way to do it.

    (defmulti getc type)
    (defmulti ++ type)
    (defmulti -- type)
    
    (derive ::bi-counter ::counter)
    
    (defn make-counter
      ([] (make-counter 0))
      ([init-val]
         (atom init-val :meta {:type ::counter})))
    
    (defn make-bi-counter
      ([] (make-bi-counter 0))
      ([init-val]
         (atom init-val :meta {:type ::bi-counter})))
    
    (defmethod getc ::counter [counter]
      @counter)
    
    (defmethod ++ ::counter [counter]
      (swap! counter inc))
    
    (defmethod -- ::bi-counter[counter]
      (swap! counter dec))
    

    e.g.

    user> (def c (make-counter))
    #'user/c
    user> (getc c)
    0
    user> (def b (make-bi-counter))
    #'user/b
    user> (++ c)
    1
    user> (++ b)
    1
    user> (-- b)
    0
    user> (-- c)
    ; Evaluation aborted.
    ;;  No method in multimethod '--' for dispatch value: :user/counter
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here are two overly simplified version of 2 tables I'm using: A: +-------+-----------------------+ |
Here are some (overly) simplified code example to describe my unit testing method. CompanyDataSet.xml
I'm using the jQueryUI (extension?) and have a simple overlay on a page. Here's
Here is the code in a function I'm trying to revise. This example works
Here is an overly simplified version of what I am trying to do: #define
I'm not overly confident in my own abilities but here goes. I want JQuery
In my web app, I'm serializing objects for storage using JSON.stringify() as described here
I'm aware that similar questions have been answered here before: postscript - overlay one
I'm trying to follow the overlay instructions provided here: https://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/LocationAwarenessPG/AnnotatingMaps/AnnotatingMaps.html#//apple_ref/doc/uid/TP40009497-CH6-SW15+ but the overlay isn't
I have an overlay that I want to hide on mousedown outside it. Here

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.