Hi Everyone,

Here's the problem I am facing,
I need to write a generator which takes any number of sequences,
interleaves them but maintains the order within each sequence.
Assume each sequence has at least one element.

For example:

(rand-interleave [1 2 3 4] [:a :b] [:A :B :C :D :E])
> => [:a 1 2 :b :A :B :C 3 :D 4 :E]


(rand-interleave [1 2 3 4])
> => [1 2 3 4]


(rand-interleave [1])
> => [1]


(rand-interleave [1] [:a] [:A])
> => [:a 1 :A]


I have been able to write this down as clojure functions.
But I am unable to convert this into a test.check generator.

Specifically:

   - How to pass random index count without using rand-int i.e. use 
   gen/choose
   - How do I write recursive functions which play well with test.check

Here's my take on it (without the generators).

(defn- first-nth
>   "(first-nth [[1 2 3 4] [:a :b :c :d]]
>                   1)
>    => :a"
>   [coll n]
>   (first (nth coll n))) 

 

(defn- next-nth
>   "(next-nth [[1 2 3 4] [:a :b :c :d]]
>                   1)
>    => [[1 2 3 4] (:b :c :d)]"
>   [coll n]
>   (->> n
>        (nth coll)
>        next
>        (assoc coll n)
>        (remove nil?)
>        vec)) 

 

(defn- rand-count
>   [coll]
>   (rand-int (count coll))) 

 

(defn- rand-interleave*
>   [coll acc]
>   (let [n (rand-count coll)]
>     (if (not-empty coll)
>       (rand-interleave* (next-nth coll n)
>                                   (conj acc
>                                           (first-nth coll n)))
>       acc))) 

 

(defn rand-interleave
>   [& args]
>   ;; Make args a vector as I would like to
>   ;; look up elements by their index values.
>   (rand-interleave* (vec args) []))


Looking forward to any suggestions on how to solve it :)

Thanks,
Mayank.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to