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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:23:03+00:00 2026-06-15T05:23:03+00:00

I’m trying to implement a Overhand Shuffle in Clojure as a bit of a

  • 0

I’m trying to implement a Overhand Shuffle in Clojure as a bit of a learning exercise

So I’ve got this code…

(defn overhand [cards]
    (let [ card_count (count cards)
          _new_cards '()
         _rand_ceiling (if (> card_count 4) (int (* 0.2 card_count)) 1)]
      (take card_count
            (reduce into (mapcat
                           (fn [c]
                             (-> (inc (rand-int _rand_ceiling))
                                 (take cards)
                                 (cons _new_cards)))
                           cards)))))

It is very close to doing what I want, but it is repeatedly taking the first (random) N number of cards off the front, but I want it to progress through the list…

calling as

(overhand [1 2 3 4 5 6 7 8 9])

instead of ending up with

(1 2 3 1 2 1 2 3 4)

I want to end up with

(7 8 9 5 6 1 2 3 4)

Also, as a side note this feels like a really ugly way to indent/organize this function, is there a more obvious way?

  • 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-15T05:23:04+00:00Added an answer on June 15, 2026 at 5:23 am

    this function is creating a list of lists, transforming each of them, and cating them back together. the problem it that it is pulling from the same thing every time and appending to a fixed value. essentially it is running the same operation every time and so it is repeating the output over with out progressing thgough the list. If you break the problem down differently and split the creation of random sized chunks from the stringing them together it gets a bit easier to see how to make it work correctly.

    some ways to split the sequence:

    (defn random-partitions [cards]
      (let [card_count (count cards)
            rand_ceiling (if (> card_count 4) (inc (int (* 0.2 card_count))) 1)]
       (partition-by (ƒ [_](= 0 (rand-int rand_ceiling))) cards)))
    

    to keep the partitions less than length four

    (defn random-partitions [cards]
      (let [[h t] (split-at (inc (rand-int 4)) cards)]
        (when (not-empty h) (lazy-seq (cons h (random-partition t))))))
    

    or to keep the partitions at the sizes in your original question

    (defn random-partitions [cards]
      (let [card_count (count cards)
            rand_ceiling (if (> card_count 4) (inc (int (* 0.2 card_count))) 1)
            [h t] (split-at (inc (rand-int rand_ceiling)) cards)]
        (when (not-empty h) (lazy-seq (cons h (random-partition t))))))
    
    (random-partitions [1 2 3 4 5 6 7 8 9 10])
    ((1 2 3 4) (5) (6 7 8 9) (10))
    

    this can also be written without directly using lazy-seq:

    (defn random-partitions [cards]
      (->> [[] cards]
           (iterate
            (ƒ [[h t]]
              (split-at (inc (rand-int 4)) t)))
           rest ;iterate returns its input as the first argument, drop it.
           (map first)
           (take-while not-empty)))
    

    which can then be reduced back into a single sequence:

    (reduce  into (random-partitions [1 2 3 4 5 6 7 8 9 10]))
    (10 9 8 7 6 5 4 3 1 2)
    

    if you reverse the arguments to into it looks like a much better shuffle

     (reduce #(into %2 %1) (random-partitions [1 2 3 4 5 6 7 8 9 10]))
    (8 7 1 2 3 4 5 6 9 10)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text

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.