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 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

Ask A Question

Stats

  • Questions 203k
  • Answers 203k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Reduce aliasing by using __restrict. Help the compiler in common… May 12, 2026 at 8:36 pm
  • Editorial Team
    Editorial Team added an answer That article doesn't post the image as an image, rather… May 12, 2026 at 8:36 pm
  • Editorial Team
    Editorial Team added an answer Does SQL Server not support UPDATE CASCADE? :- Foreign Key… May 12, 2026 at 8:36 pm

Related Questions

So, bitfields. Specifically, large bitfields. I understand how to manipulate individual values in a
I'm not quite sure how to describe this in words without making it overly
I am attempting to implement a custom view. This view should display an image
I'm calling a webservice that is returning an unknown quantity of images in the
Every programming language I know (Perl, Javascript, PHP, Python, ASP, ActionScript, Commodore Basic) uses

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.