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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:20:07+00:00 2026-05-25T17:20:07+00:00

I am working on the solution for this koan , which states: Write a

  • 0

I am working on the solution for this koan, which states:

Write a function which replicates each element of a sequence a variable number of times.

To do this, I want to:

  1. Make a method which takes a sequence and the number of times to repeat each element.
  2. Define a local method within this method which duplicates a value, v, n times into a sequence.

With that in mind, I wrote the following method:

(fn dupSeqX [aseq x]
  (fn dupx [v x]
    (if (= x 1) (list v) (concat v (dupx v (- x 1))))
  )
  (reverse (reduce #(concat %1 (dupx %2 x)) '() aseq)))

When running this code, I get the following error:

java.security.PrivilegedActionException: java.lang.Exception: Unable to resolve symbol: dupx in this context (NO_SOURCE_FILE:0)

How do I go about creating a local method that will allow me to finish this koan?

Is there a “clojure-esque” way of doing this that I’m not aware of?

  • 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-25T17:20:08+00:00Added an answer on May 25, 2026 at 5:20 pm

    First of all, we don’t talk about methods: we talk about functions. There is something in clojure that you could call a method but it’s different from a function. If you stop using the OO lingo you will lose the OO style of thinking too.

    What you are trying to do is possible. You basiclly want to create a new function with the name dupx in the dupseqx function. What you are doing right now is creating a function and then throwing it away (you don’t do anything with the return value and only the last form in a function gets returned). Since a function is just like any other value, you can use the same mecanism like with any other value: create a local “variable”. What’s the mechanism for this? It’s local binding and it works like this (The name in the fn is just so that you can call it from itself; it doesn’t need to be the same as the let-bound name):

    (let [dupx (fn dupx [v x] 
                 (if (= x 1) 
                      (list v) 
                      (cons v (dupx v (dec x)))))]
      (dupx 5 3))
    

    Notice that I corrected some other things.

    A shorter form of this (fixing the double name ugliness):

    (letfn [(dupx [v x] (if (= x 1)
                           (list v) 
                           (cons v (dupx v (dec x)))))]
      (dupx 5 3))
    

    Ok in everything between “(let […]” and the matching “)” we now have a dupx function.

    So now the rest of your code works:

    (fn dupSeqX [aseq x]
      (letfn [(dupx [v x] (if (= x 1) (list v) (cons v (dupx v (dec x)))))]
        (reverse (reduce #(concat %1 (dupx %2 x)) '() aseq))))
    

    This code can be made a little more idiomatic:

    • Coding Guidelines: name parameter coll instead of aseq
    • Coding Guidelines: DoNotUseCamalCase do-it-like-this
    • Recursion when you don’t need it is bad for performence and big numbers.
    • You are reinventing the wheel. This is good to learn coding but not good if you wnat to get to know the language and the standard lib.

    How did I go about writing this?

    First the basic fn. coll is the standard for naming function that expect sequences.

    (fn [coll times]  )
    

    If you read this “each element of a sequence” your brain has to go MAP.

    (fn [coll times] 
       (map (fn ....) coll))
    

    “replicates each … ” is basically a description of what you have to put into the map function. We can use repeat (your dubx function but with some extra goodies like that it’s lazy).

    (fn [coll times] 
       (map (fn [val] (repeat times val)) coll))
    

    There is one problem left (from the koan). It wants one seq back, not a sequence in a sequence for each element. This means we have to concat the result together.

     (fn [coll times] 
       (apply concat (map (fn [val] (repeat times val)) coll)))
    

    You will often see the (apply concat (map ....)) pattern. There is a better function for that in the standard library, called mapcat, and I’ll make the inner function into the short syntax.

     (fn [coll times] 
       (mapcat #(repeat times %) coll))
    

    Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on the solution to this koan . I'm stumped as to why
I'd like to create this function, which selects a random element from a Set
Has anyone else had this issue and found a working solution? I've enabled the
Update: This question was an epic failure, but here's the working solution. It's based
As kind of a followup to this question I've gotten a solution working on
I'm working with a large (270+ project) VS.Net solution. Yes, I know this is
Does anyone have the same problem or a working solution? I get always this
I had this solution working happily and i added this attribute: [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct,
This question is mainly out of curiosity (I have a different working solution to
I haven't seen working solution for Drupal 7 to do this. I have an

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.