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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:34:19+00:00 2026-06-07T18:34:19+00:00

In my answer to Clojure For Comprehension example , there is some duplication that

  • 0

In my answer to Clojure For Comprehension example, there is some duplication that I would like to remove:

(def all-letters (map char (range 65 90)))
(defn kw [& args] (keyword (apply str args)))
(concat
  (for [l all-letters] (kw l))
  (for [l all-letters l2 all-letters] (kw l l2))
  (for [l all-letters l2 all-letters l3 all-letters] (kw l l2 l3)))

I would like to remove the “for” duplication. I have written the following macro:

(defmacro combine [times]
 (let [symbols (repeatedly times gensym)
       for-params (vec (interleave symbols (repeat 'all-letters)))]
    `(for ~for-params (kw ~@symbols))))

Which works with:

 (concat (combine 1) (combine 2) (combine 3))

But if I try:

 (for [i (range 1 4)] (combine i))

I get:

CompilerException java.lang.ClassCastException: clojure.lang.Symbol cannot be cast to java.lang.Number, compiling:(NO_SOURCE_PATH:177) 

What is going on? Is there a better way of removing the duplication?

  • 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-07T18:34:20+00:00Added an answer on June 7, 2026 at 6:34 pm

    Your problem is that combine is a macro that gets expanded at compile time. When you try to expand on a symbol i it fails, because it is designed to take a number (times). i is just a symbol at compile time, it only evaluates to numeric values at runtime.

    I’d suggest rewriting combine to be a function rather than a macro: you don’t really need macros here and functions are frequently more convenient (as in this case!).

    Here’s a recursive combine that probably does roughly what you want:

    (defn combine
        ([times] (combine times nil))
        ([times letters]
          (if (<= times 0)
            (keyword (apply str letters))
            (flatten (for [l all-letters] (combine (dec times) (cons l letters)))))))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to embed Clojure code into Java. This site was helpful in
The answer to What is the correct way to make exceptions serializable? says that
My answer to this problem feels too much like these solutions in C .
How to implement 10k connections echo server in Clojure? clojure.contrib.server-socket is not the answer
In Clojure you can unquote-splice a list of values to generate code, e.g. (def
I never really thought about this until I was explaining some clojure code to
I am writing some lesson materials for a course that starts in a couple
I've spent some time, trying to understand Clojure multimethods. The main pro multimethod argument,
I want to use JOGL (for Clojure, not Java). There seems to be quite
I'm writing some clojure code, and I'm relying on Joda time for time handling.

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.