This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-04-05
Channels
- # announcements (7)
- # beginners (10)
- # calva (14)
- # clj-otel (8)
- # clojure (42)
- # clojure-europe (20)
- # clojure-nl (1)
- # clojure-norway (22)
- # clojure-spec (8)
- # clojure-uk (7)
- # core-async (10)
- # cursive (1)
- # events (1)
- # hyperfiddle (20)
- # introduce-yourself (1)
- # jobs-discuss (11)
- # lsp (48)
- # missionary (3)
- # music (1)
- # off-topic (7)
- # overtone (9)
- # pedestal (21)
- # rdf (1)
- # releases (3)
- # shadow-cljs (22)
- # specter (13)
- # squint (1)
- # yamlscript (3)
Any suggestions on how I might make a lazy infinite (or representable as a transducer) version of this function such that it can be parameterless? I'm not super familiar with crafting lazy seqs.
(defn random-nums
"Returns a collection of random integers in the domain of [1, 20]. The first
20 are guaranteed to be unique among each other, as well as the next 20,
and so on. Only returns the first x integers."
[x]
(->> (range 1 21)
(repeat (ceil (/ x 20)))
(sequence (comp (map shuffle) cat (take x)))))
I would probably do something like:
(defn random-nums
"Returns a collection of random integers in the domain of [1, 20]. The first
20 are guaranteed to be unique among each other, as well as the next 20,
and so on. Only returns the first x integers."
[]
(eduction
(mapcat (fn [[start end]]
(shuffle (range start end))))
(repeat [1 21])))
> (take 25 (random-nums))
(11 20 4 17 6 14 5 15 8 1 2 12 7 13 16 10 18 3 19 9 13 15 17 8 9)
Ahh thats it, I was almost there thanks a bunch!
eduction
won't return a lazy-seq. which can lead to situations like
(let [x (random-nums)]
[(first x) (first x)]) ;; => [6 16]
You might prefer sequence
when applying the transducer
(defn random-nums []
(->> (repeat (range 1 21))
(sequence (comp (map shuffle) cat))))
👍 1
Another way to express the same thing...
(def one-to-20 (range 1 21))
(defn rand-20s [n]
(take n (mapcat identity
(repeatedly #(shuffle one-to-20)))))
user=> (rand-20s 20)
(10 3 2 5 14 1 9 20 15 16 13 19 7 17 4 12 6 11 18 8)
user=> (rand-20s 20)
(11 1 6 10 14 5 4 19 16 3 12 8 7 15 9 13 18 17 2 20)
user=> (rand-20s 20)
(20 4 3 5 13 14 6 19 17 11 7 10 18 2 15 9 12 16 8 1)
Wondering... maybe we can have cake and eat educe it too?
(def one-to-20 (range 1 21))
(defn shuffled-20s []
(mapcat identity (repeatedly #(shuffle one-to-20))))
(defn educe-rand-20s [n coll]
(eduction (take n) coll))
user=> (educe-rand-20s 20 (shuffled-20s))
(11 17 1 15 13 6 16 18 20 10 4 3 2 5 19 8 14 12 7 9)
user=> (educe-rand-20s 20 (shuffled-20s))
(1 4 11 16 9 14 12 13 18 20 19 6 8 17 10 7 3 2 5 15)