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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:07:52+00:00 2026-05-12T22:07:52+00:00

I have a group of numeric functions in Clojure that I want to validate

  • 0

I have a group of numeric functions in Clojure that I want to validate the arguments for. There are numerous types of arguments expected by the functions, such as positive integers, percentages, sequences of numbers, sequences of non-zero numbers, and so on. I can validate the arguments to any individual function by:

  1. Writing validation code right into
    the function.
  2. Writing a general
    purpose function, passing it the
    arguments and expected types.
  3. Writing a general purpose macro,
    passing it the arguments and
    expected types.
  4. Others I haven’t thought of.

Some Lisp code by Larry Hunter is a nice example of #3. (Look for the test-variables macro.)

My intuition is that a macro is more appropriate because of the control over evaluation and the potential to do compile-time computation rather than doing it all at run time. But, I haven’t run into a use case for the code I’m writing that seems to require it. I’m wondering if it is worth the effort to write such a macro.

Any suggestions?

  • 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-12T22:07:53+00:00Added an answer on May 12, 2026 at 10:07 pm

    Clojure already has (undocumented, maybe subject-to-change) support for pre- and post-conditions on fns.

    user> (defn divide [x y]
            {:pre [(not= y 0)]}
            (/ x y))
    user> (divide 1 0)
    Assert failed: (not= y 0)
       [Thrown class java.lang.Exception]
    

    Kind of ugly though.

    I’d probably write a macro just so I could report which tests failed in a succinct way (quote and print the test literally). The CL code you linked to looks pretty nasty with that enormous case statement. Multimethods would be better here in my opinion. You can throw something like this together pretty easily yourself.

    (defmacro assert* [val test]
      `(let [result# ~test]              ;; SO`s syntax-highlighting is terrible
         (when (not result#)
           (throw (Exception.
                   (str "Test failed: " (quote ~test)
                        " for " (quote ~val) " = " ~val))))))
    
    (defmulti validate* (fn [val test] test))
    
    (defmethod validate* :non-zero [x _]
      (assert* x (not= x 0)))
    
    (defmethod validate* :even [x _]
      (assert* x (even? x)))
    
    (defn validate [& tests]
      (doseq [test tests] (apply validate* test)))
    
    (defn divide [x y]
      (validate [y :non-zero] [x :even])
      (/ x y))
    

    Then:

    user> (divide 1 0)
    ; Evaluation aborted.
    ; Test failed: (not= x 0) for x = 0
    ;   [Thrown class java.lang.Exception]
    
    user> (divide 5 1)
    ; Evaluation aborted.
    ; Test failed: (even? x) for x = 5
    ;   [Thrown class java.lang.Exception]
    
    user> (divide 6 2)
    3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 240k
  • Answers 240k
  • 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 See Time Complexity. The python dict is a hashmap, its… May 13, 2026 at 7:13 am
  • Editorial Team
    Editorial Team added an answer When I read your question it seems like the shippinglabel-solution… May 13, 2026 at 7:13 am
  • Editorial Team
    Editorial Team added an answer @duffymo's idea about not keeping connections open too long is… May 13, 2026 at 7:13 am

Related Questions

I want to compute a checksum of all of the values of a column
I have a dataframe, and I want to produce a table of summary statistics
I have a SQL Table UDF That gets a Standard Deviation from a 20
I have the unfortunate task of cleaning up a bunch of old ColdFusion code.

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.